Time Date String + TimeZone

I have a string representation of local date time and Java TimeZone.

I am trying to get output in the format MM / dd / yyyy HH: mm: ssZ, but I cannot figure out how to create a Calendar or JodaTime object with the correct date and time zone. How do you get a TimeZone converted to a value that can be parsed using SimpleDateFormat 'Z' or 'z'?

TimeZone tz = TimeZone.getTimeZone("America/Chicago"); String startDate = "08/14/2014 15:00:00"; SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); Calendar cal = Calendar.getInstance(tz); cal.setTime(sdf.parse(startDate)); SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ssZ"); 

and

 sdfZ.format(cal.getTime()) 

returns

 08/14/2014 15:00:00-0400 

which is equal to EST .

The only workaround to create a Calendar or Joda DateTime and set individual year / month / day / hour / min values ​​by parsing the string "08/14/2014 15:00:00"?

+2
source share
2 answers

Calendar getTime() - returns a Date object representing this calendar time value (millisecond offset from the epoch (01-01-1970 00:00 GMT) "), regardless of what time zone you are displaying. But the hour of the day in different TimeZones will be be different.get get(Calendar.HOUR_OF_DAY)

You must try

 sdfZ.setTimeZone(tz); 
+1
source

TL; DR

 ZonedDateTime zdt = ZonedDateTime.now( ZoneId.of( "America/Chicago" ) ) ; String output = zdt.toInstant().toString() ; 

2016-12-03T10: 15: 30z

java.time

Both the java.util.Calendar classes and the Joda-Time library were superseded by the java.time classes.

Instant

Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds .

 Instant instant = Instant.now(); 

Call toString to generate a string in the standard ISO 8601 format . For example, 2011-12-03T10:15:30Z . This format is good for serializing date and time values ​​for storing or exchanging data.

 String output = instant.toString(); // Ex: 2011-12-03T10:15:30Z 

Timezone

Set the time zone.

 ZoneId z = ZoneId.of( "America/Chicago" ); ZonedDateTime zdt = instant.atZone( z ); 

As a shortcut, you can skip using Instant .

 ZonedDateTime zdt = ZonedDateTime.now( z ); 

Calling toString on ZonedDateTime provides you with an extended version of the standard ISO 8601 format, where the time zone name is added in square brackets. For example, 2007-12-03T10:15:30+01:00[Europe/Paris] .

 String output = zdt.toString(); // Ex: 2007-12-03T10:15:30+01:00[Europe/Paris] 

DateTimeFormatter

The DateTimeFormatter class has a predefined formatting constant for the desired result: DateTimeFormatter.ISO_LOCAL_DATE_TIME

 String output zdt.format( DateTimeFormatter.ISO_LOCAL_DATE_TIME ); 

About java.time

The java.time framework is built into Java 8 and later. These classes supersede the nasty old legacy time classes such as java.util.Date , Calendar and SimpleDateFormat .

The Joda-Time project, now in maintenance mode , we recommend switching to the java.time classes.

To learn more, see the Oracle Tutorial . And search for qaru for many examples and explanations. JSR 310 specification .

With a JDBC driver corresponding to JDBC 4.2 or later, you can exchange java.time objects directly with your database. No need for strings or java.sql. * Classes.

Where to get java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is proof of possible future additions to java.time. Here you can find useful classes such as Interval , YearWeek , YearQuarter and more .

0
source

Source: https://habr.com/ru/post/975191/


All Articles