convert java.util.Date to java.util.Date with different formating in JAVA [duplicate]

我是研究僧i 提交于 2021-02-05 10:46:28

问题


I get Date as java.util.Date(not String) : (java.util.Date) Mon Jul 13 00:00:00 IST 2020

I want to convert it to : 2020-07-13T00:00 format==>("yyyy-MM-dd'T'HH:mm") but as DATE not String.

I tried following code:

Date scheduleDate=details.getScheduledDate();                       // This value is fetched from object passed-- [scheduleDate = (java.util.Date) Mon Jul 13 00:00:00 IST 2020]

SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
sd.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
String dateFormat=sd.format(scheduleDate);                           //Here I get [dateFormat = (java.lang.String) "2020-07-13T00:00"]
Date date = sd.parse(dateFormat);                                    //[date = (java.util.Date) Mon Jul 13 00:00:00 IST 2020]

I observed that string format has correct(as expected ) value but the value changes back when I convert it to java.util.date.

Does java.util.Date support yyyy-MM-dd'T'HH:mm format ?

If yes, Can anyone suggest me with any good approach/direction/topics/library to look into.

Thank You..


回答1:


tl;dr

Convert from legacy class to modern class. Adjust from UTC to a time zone. Generate text in standard ISO 8601. We omit the context of time zone or offset in our output because you so requested, against my recommendation.

myJavaUtilDate
.toInstant()
.atZone( ZoneId.of( "Asia/Kolkata" ) )
.truncatedTo( ChronoUnit.MINUTES ) 
.format( DateTimeFormatter.ISO_LOCAL_DATE_TIME )

I expect using UTC and including the offset would be wiser.

myJavaUtilDate
.toInstant()
.toString()

Details

Date-time objects do not have a format, only text has a format.

Use java.time classes, never java.util.Date.

Convert your legacy Date object to its modern replacement, java.time.Instant.

Instant instant = myJUDate.toInstant() ;

Adjust from UTC to your desired time zone.

ZoneId z = ZoneId.of( "Asia/Kolkata" ) ; 
ZonedDateTime zdt = instant.atZone( z ) ;

Apparently you do not care about the the second of minute. So let’s truncate that to zero seconds.

ZonedDateTime zdt = zdt.truncatedTo( ChronoUnit.MINUTES ) ;

Generate text in your desired format. Java comes bundled with a formatter already defined for your format.

String output = zdt.format( DateTimeFormatter.ISO_LOCAL_DATE_TIME ) ;

I showed that format because asked. But I do not recommend it. That format fails to indicate a time zone or offset-from-UTC. So if it says noon, the reader does not know if that means noon in Tokyo Japan 🇯🇵, noon in Toulouse France 🇫🇷, or noon in Toledo Ohio Us 🇺🇸 — three very different moments, several hours apart.

When communicating a moment, a specific point on the timeline, textually it is usually best to do so in UTC. And use ISO 8601 standard formats. Commonly a Z is placed on the end to indicate UTC, an offset of zero hours-minutes-seconds.

Instant instant = zdt.toInstant() ;
String output = instant.toString() ;


来源:https://stackoverflow.com/questions/62871262/convert-java-util-date-to-java-util-date-with-different-formating-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!