18

Possible Solution:Convert Java Date into another Time as Date format

I went through it but does not get my answer.

I have a string "2013-07-17T03:58:00.000Z" and I want to convert it into date of the same form which we get while making a new Date().Date d=new Date();

The time should be in IST Zone - Asia/Kolkata

Thus the date for the string above should be

Wed Jul 17 12:05:16 IST 2013 //Whatever Time as per Indian Standard GMT+0530

String s="2013-07-17T03:58:00.000Z";
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); 
TimeZone tx=TimeZone.getTimeZone("Asia/Kolkata");
formatter.setTimeZone(tx);
d= (Date)formatter.parse(s);

3 Answers 3

26

Use calendar for timezones.

TimeZone tz = TimeZone.getTimeZone("Asia/Calcutta");
Calendar cal = Calendar.getInstance(tz);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
sdf.setCalendar(cal);
cal.setTime(sdf.parse("2013-07-17T03:58:00.000Z"));
Date date = cal.getTime();

For this however I'd recommend Joda Time as it has better functions for this situation. For JodaTime you can do something like this:

DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
DateTime dt = dtf.parseDateTime("2013-07-17T03:58:00.000Z");
Date date = dt.toDate();
Sign up to request clarification or add additional context in comments.

5 Comments

Does it retun me a Date Object?
Edited so last line is returning it to date. However note that Date has no timezone.
But joda Time jar file is heavy.And including it just for converting date once make sense?
This is why I also included something that uses calendar, in case you didn't want to use Joda Time?
After a long search, I found this is the only working solution. Thanks @MarkM
4

A Date doesn't have any time zone. If you want to know what the string representation of the date is in the indian time zone, then use another SimpleDateFormat, with its timezone set to Indian Standard, and format the date with this new SimpleDateFormat.

EDIT: code sample:

String s = "2013-07-17T03:58:00.000Z";
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
Date d = formatter.parse(s);

System.out.println("Formatted Date in current time zone = " + formatter.format(d));

TimeZone tx=TimeZone.getTimeZone("Asia/Calcutta");
formatter.setTimeZone(tx);
System.out.println("Formatted date in IST = " + formatter.format(d));

Output (current time zone is Paris - GMT+2):

Formatted Date in current time zone = 2013-07-17T05:58:00.000+02
Formatted date in IST = 2013-07-17T09:28:00.000+05

3 Comments

Hii..I have edited my code..But Its saying unable to parse the string.
SimpleDateFormat string should be "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" otherwise got java.lang.IllegalArgumentException: Unknown pattern character 'X'
@VijayC That depends on your Java version. Z means offset zero or “Zulu time zone”, so you should prefer to make sure you parse it as such.
0

java.time

I should like to provide the modern answer. And give you a suggestion: rather than reproducing what Date.toString() would have given you, better to use the built-in format for users in the relevant locale:

    String isoString = "2013-07-17T03:58:00.000Z";
    String humanReadable = Instant.parse(isoString)
            .atZone(userTimeZone)
            .format(localizedFormatter);
    System.out.println(humanReadable);

On my computer this prints

17 July 2013 at 09.28.00 IST

My snippet uses a couple of constants

private static final ZoneId userTimeZone = ZoneId.of("Asia/Kolkata");
private static final DateTimeFormatter localizedFormatter = DateTimeFormatter
        .ofLocalizedDateTime(FormatStyle.LONG)
        .withLocale(Locale.getDefault(Locale.Category.FORMAT));

The withLocale call is redundant when we just pass the default format locale. I put it in so you have a place to provide an explicit locale, should your users require a different one. It also has the nice advantage of making explicit that the result depends on locale, in case someone reading your code didn’t think of that.

I am using java.time, the modern Java date and time API. It is so much nicer to work with than the outdated Date, TimeZone and the notoriously troublesome DateFormat and SimpleDateFormat. Your string conforms to the ISO 8601 standard, a format that the modern classes parse as their default, that is, without any explicit formatter.

Same format as Date gave you

Of course you can have the same format as the old-fashioned Date would have given you if you so require. Just substitute the formatter above with this one:

private static final DateTimeFormatter asUtilDateFormatter 
        = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ROOT);

Now we get:

Wed Jul 17 09:28:00 IST 2013

Link to tutorial

Read more about using java.time in the Oracle tutorial and/or find other resources out there.

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.