如何将日历日期转换为yyyy-MM-dd格式。

Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 1);
Date date = cal.getTime();             
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
String date1 = format1.format(date);            
Date inActiveDate = null;
try {
    inActiveDate = format1.parse(date1);
} catch (ParseException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

这将产生inActiveDate = Wed september 26 00:00:00 IST 2012。但我需要的是2012-09-26。我的目的是使用Hibernate标准将这个日期与数据库中的另一个日期进行比较。我需要date对象的格式是yyyy-MM-dd。


当前回答

java.time

MadProgrammer的回答是正确的,尤其是关于Joda-Time的建议。Joda-Time的后继者现在作为新的Java内置到Java 8中。包的时间。下面是Java 8中的示例代码。

When working with date-time (as opposed to local date), the time zone in critical. The day-of-month depends on the time zone. For example, the India time zone is +05:30 (five and a half hours ahead of UTC), while France is only one hour ahead. So a moment in a new day in India has one date while the same moment in France has “yesterday’s” date. Creating string output lacking any time zone or offset information is creating ambiguity. You asked for YYYY-MM-DD output so I provided, but I don't recommend it. Instead of ISO_LOCAL_DATE I would have used ISO_DATE to get this output: 2014-02-25+05:30

ZoneId zoneId = ZoneId.of( "Asia/Kolkata" );
ZonedDateTime zonedDateTime = ZonedDateTime.now( zoneId );

DateTimeFormatter formatterOutput = DateTimeFormatter.ISO_LOCAL_DATE; // Caution: The "LOCAL" part means we are losing time zone information, creating ambiguity.
String output = formatterOutput.format( zonedDateTime );

转储到控制台…

System.out.println( "zonedDateTime: " + zonedDateTime );
System.out.println( "output: " + output );

运行时……

zonedDateTime: 2014-02-25T14:22:20.919+05:30[Asia/Kolkata]
output: 2014-02-25

乔达时间

使用Joda-Time库(java.time的前身)的类似代码。

DateTimeZone zone = new DateTimeZone( "Asia/Kolkata" );
DateTime dateTime = DateTime.now( zone );
DateTimeFormatter formatter = ISODateTimeFormat.date();
String output = formatter.print( dateTime );

ISO 8601

顺便说一下,输入字符串的格式是标准格式,是ISO 8601定义的几种方便的日期-时间字符串格式之一。

Joda-Time和java。在解析和生成各种日期时间值的字符串表示形式时,默认使用ISO 8601格式。

其他回答

你的代码是错误的。没有必要解析日期并将其保留为date对象。

当您希望将日历日期对象显示为字符串时,可以对其进行格式化。

Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 1);
Date date = cal.getTime();             
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");          
String inActiveDate = null;
try {
    inActiveDate = format1.format(date);
    System.out.println(inActiveDate );
} catch (ParseException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

我的答案是kotlin语言。

您可以使用SimpleDateFormat来实现如下结果:

val date = Date(timeInSec)
val formattedDate = SimpleDateFormat("yyyy-MM-dd", Locale("IN")).format(date)

详情请点击这里。

OR

使用日历为您做:

val dateObject = Date(timeInMillis)
val calendarInstance = Calendar.getInstance()
calendarInstance.time = dateObject
val date = "${calendarInstance.get(Calendar.YEAR)}-${calendarInstance.get(Calendar.MONTH)}-${calendarInstance.get(Calendar.DATE)}"

要了解更多细节,请查看这个答案。

为了解析java.util.Date对象,您必须首先使用自己的格式将其转换为String。

inActiveDate = format1.parse(  format1.format(date)  );

但我认为你在这里是多余的。

java.time

MadProgrammer的回答是正确的,尤其是关于Joda-Time的建议。Joda-Time的后继者现在作为新的Java内置到Java 8中。包的时间。下面是Java 8中的示例代码。

When working with date-time (as opposed to local date), the time zone in critical. The day-of-month depends on the time zone. For example, the India time zone is +05:30 (five and a half hours ahead of UTC), while France is only one hour ahead. So a moment in a new day in India has one date while the same moment in France has “yesterday’s” date. Creating string output lacking any time zone or offset information is creating ambiguity. You asked for YYYY-MM-DD output so I provided, but I don't recommend it. Instead of ISO_LOCAL_DATE I would have used ISO_DATE to get this output: 2014-02-25+05:30

ZoneId zoneId = ZoneId.of( "Asia/Kolkata" );
ZonedDateTime zonedDateTime = ZonedDateTime.now( zoneId );

DateTimeFormatter formatterOutput = DateTimeFormatter.ISO_LOCAL_DATE; // Caution: The "LOCAL" part means we are losing time zone information, creating ambiguity.
String output = formatterOutput.format( zonedDateTime );

转储到控制台…

System.out.println( "zonedDateTime: " + zonedDateTime );
System.out.println( "output: " + output );

运行时……

zonedDateTime: 2014-02-25T14:22:20.919+05:30[Asia/Kolkata]
output: 2014-02-25

乔达时间

使用Joda-Time库(java.time的前身)的类似代码。

DateTimeZone zone = new DateTimeZone( "Asia/Kolkata" );
DateTime dateTime = DateTime.now( zone );
DateTimeFormatter formatter = ISODateTimeFormat.date();
String output = formatter.print( dateTime );

ISO 8601

顺便说一下,输入字符串的格式是标准格式,是ISO 8601定义的几种方便的日期-时间字符串格式之一。

Joda-Time和java。在解析和生成各种日期时间值的字符串表示形式时,默认使用ISO 8601格式。

Java Date是一个容器,用于记录自1970年1月1日00:00:00 GMT以来的毫秒数。

当您使用System.out.println(date)之类的东西时,Java使用date . tostring()来打印内容。

改变它的唯一方法是重写Date并提供自己的Date. tostring()实现。现在,在您启动IDE并尝试此操作之前,我不会这样做;这只会使事情复杂化。您最好将日期格式化为您想要使用(或显示)的格式。

Java 8 +

LocalDateTime ldt = LocalDateTime.now().plusDays(1);
DateTimeFormatter formmat1 = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.ENGLISH);
System.out.println(ldt);
// Output "2018-05-12T17:21:53.658"

String formatter = formmat1.format(ldt);
System.out.println(formatter);
// 2018-05-12

Java 8之前

你应该使用ThreeTen Backport

出于历史原因,保留以下内容(作为原始答案)

你能做的就是格式化日期。

Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 1);
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(cal.getTime());
// Output "Wed Sep 26 14:23:28 EST 2012"

String formatted = format1.format(cal.getTime());
System.out.println(formatted);
// Output "2012-09-26"

System.out.println(format1.parse(formatted));
// Output "Wed Sep 26 00:00:00 EST 2012"

这其实是同一个日期,只是表示方式不同。