如何将日历日期转换为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。


当前回答

我的答案是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.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对象不能以自定义格式表示日期,而必须使用SimpleDateFormat。返回字符串的格式方法。

String myString=format1.format(date);

我发现了这段代码,其中日期以一种格式与数据库中的日期字段进行比较…也许这对你有帮助…

当您使用simpledateformat将字符串转换为日期时,它很难与mysql数据库中的日期字段进行比较。

所以转换java字符串日期格式使用select STR_to_DATE('yourdate','%m/%d/%Y')——>在这种格式,然后你将得到mysql日期字段的确切日期格式。

http://javainfinite.com/java/java-convert-string-to-date-and-compare/

public static void main(String[] args) {
    Calendar cal = Calendar.getInstance();
    cal.set(year, month, date);
    SimpleDateFormat format1 = new SimpleDateFormat("yyyy MM dd");
    String formatted = format1.format(cal.getTime());
    System.out.println(formatted);
}

我的答案是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)}"

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