6
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 is happening::

  • At the start

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

  • At the end

needbydate= Dec/18/2014

17 is changed to 18 .... What wrong am i doing in 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()+"";
2
  • can you try with HH instead of hh? Capital h is for the 24hours format Commented Dec 17, 2014 at 11:17
  • 1
    I am getting the 17th fine but I have no Z on the SDF as it is causing an Unparseable date exception. Commented Dec 17, 2014 at 11:26

3 Answers 3

6

Your date formats are using the system default time zone. That's okay for your input, because it specifies the UTC offset explicitly - but for your output, you've just got a date. So it's showing you the date that that point in time occurred in your system time zone.

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

You should also be using HH in your input format instead of hh, as it's clearly a 24-hour value rather than a 12-hour value.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the input .... Please see the edit .... Is this what you are refering ... I have used jodaLibrary for this ... How can i learn about these conversions any resource(I beginner in this concepts)?
@Devrath: Well no, I never suggested moving to Joda Time, although that's generally better. It's not clear why you're using a specific time zone here, or what's wrong with what I actually suggested. If you do want to use Joda Time, you'd be better off using a DateTimeFormatter for the parsing, instead of passing the string into the constructor.
Oh ... k how to use DateFormat.setTimeZone any sample .... i am not able to read the docs and imprint it (newbie to these concepts )
@Devrath: you call it with the time zone you want the output to use. I don't know what else there is to say...
@Devrath To learn Joda-Time, simply search here on StackOverflow for "joda". You will find hundreds of practical examples. Much easier to learn when you see problem->solution rather than just reading the JavaDoc.
1

With the help of 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 Comment

Why the concatenation of an empty string at the end?
1

Joda-Time

Much easier in Joda-Time indeed. Joda-Time handles that standard ISO 8601 format string by default.

When the input string includes an offset (+00:00 in this case), the rest of the string is parsed accordingly. When passing a time zone object at the same time, Joda-Time adjusts the parsed value to that zone. See the code example below for a demo.

Note how the adjustment for Kolkata India means the date rolls over 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 adjust 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 to 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 );

When run.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.