Error converting date in Java
String date = jsonobject.getString("needbydate");
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 up?
>At first
date = 2014-12-17T21:37:00 00:00
>Finally
Needbydate = December 18, 2014
Change 17 to 18 What did I do wrong 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()+"";
Solution
Your date format uses the system default time zone This is OK for your input because it explicitly specifies the UTC offset - but for your output, you just get a date Therefore, it displays the date of that time point in your system time zone
You need to consider the time zone you want - and whether your input is affected by non - zero offset You can use dateformat Settimezone sets the time zone to use when exporting (for example, should 2014-12-17t21:37:00-05:00 be displayed as December 18 (UTC) or December 17 (source time zone)
You should also use HH in the input format instead of HH, because it is obviously a 24-hour value rather than a 12-hour value