1

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..

6
  • 7
    java.util.Date itself does not hold any information about formatting. You can't set format of it. You can set format only when you want to display it, aka convert to String. Btw. You probably should move to a "new" Java Date/Time API that was added in java 8 (in package java.time) Commented Jul 13, 2020 at 7:45
  • 1
    I am just curious, why u wanna do that? Commented Jul 13, 2020 at 7:50
  • @jagapathi : I need date in that format to pass to a react JS application with material UI datetime-local calender. Commented Jul 13, 2020 at 7:55
  • 1
    @asha u need to convert the date in react js to any formate you need. Commented Jul 13, 2020 at 7:57
  • 2
    The industry standard is that in your JSON transmission, you should use ISO 8601 formatting (preferably in UTC with Z), and then do your formatting on the client side. The default JS library for this seems to be Moment. Commented Jul 13, 2020 at 8:11

1 Answer 1

6

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() ;
Sign up to request clarification or add additional context in comments.

1 Comment

This is really helpful... Thanks!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.