Error in converting dates in java

String date = jsonobject.getString("needbydate"); DateFormat df = new SimpleDateFormat("MMM/dd/yyyy"); DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ"); Date startDate = sdf.parse(date); String needbydate = df.format(startDate).toString()+""; 

What's happening::

  • At the beginning

date = 2014-12-17T21:37:00+00:00

  • In the end

needbydate = Dec / needbydate

17 changed to 18 .... What am I doing in the conversion


EDIT:

  String date=jsonobject.getString("needbydate"); DateFormat df = new SimpleDateFormat("MMM/dd/yyyy",Locale.ENGLISH); DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss",Locale.ENGLISH); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); Date startDate; startDate = sdf.parse(date); needbydate = df.format(startDate).toString()+""; 
+6
source share
3 answers

Your date formats use the default system time zone. This is good for your input because it explicitly sets the UTC offset, but for your output you just got a date. Thus, it shows you the date when this point in time occurred in your system time zone.

You need to think about what time zone you need - and whether it depends on a non-zero offset at your input. You can use DateFormat.setTimeZone to set the time zone to be used on the output. (For example, should 2014-12-17T21: 37: 00-05: 00 show December 18th (UTC) or December 17th (the original time zone)?)

You should also use HH in your input format instead of HH , since this is clearly a 24-hour value, not a 12-hour value.

+6
source

Using JonSkeet resolved this ... Complete solution ... here

  String date=jsonobject.getString("needbydate"); DateFormat df = new SimpleDateFormat("MMM/dd/yyyy",Locale.ENGLISH); df.setTimeZone(TimeZone.getTimeZone("UTC")); DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss",Locale.ENGLISH); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); Date startDate; startDate = sdf.parse(date); needbydate = df.format(startDate).toString(); 
+1
source

Joda time

Much easier in Joda-Time . Joda-Time processes the default ISO 8601 standard string.

When the input string includes an offset ( +00:00 in this case), the rest of the string is parsed accordingly. While passing the time zone object, Joda-Time corrects the analyzed value in this zone. Below is a sample code for demonstration.

Notice how the adjustment for Calcutta in India means that the date flipped from the 17th to the 18th.

 String input = "2014-12-17T21:37:00+00:00"; DateTimeZone zone = DateTimeZone.forID( "America/Montreal" ); DateTime dateTimeMontréal = new DateTime( input , zone ); // Parse as UTC because of "+00:00" offset, then adjust to desired time zone of Montréal. 

Easily adjusts to other time zones.

 DateTime dateTimeParis = dateTimeMontréal.withZone( DateTimeZone.forID( "Europe/Paris" ) ); DateTime dateTimeUtc = dateTimeMontréal.withZone( DateTimeZone.UTC ); DateTime dateTimeKolkata = dateTimeMontréal.withZone( DateTimeZone.forID( "Asia/Kolkata" ) ); 

Dump for the console.

 System.out.println( "input: " + input ); System.out.println( "dateTimeMontréal: " + dateTimeMontréal ); System.out.println( "dateTimeParis: " + dateTimeParis ); System.out.println( "dateTimeUtc: " + dateTimeUtc ); System.out.println( "dateTimeKolkata: " + dateTimeKolkata ); 

At startup.

 input: 2014-12-17T21:37:00+00:00 dateTimeMontréal: 2014-12-17T16:37:00.000-05:00 dateTimeParis: 2014-12-17T22:37:00.000+01:00 dateTimeUtc: 2014-12-17T21:37:00.000Z dateTimeKolkata: 2014-12-18T03:07:00.000+05:30 
+1
source

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


All Articles