iam stack
iam stack

Reputation: 97

Format a date String java

I have a date String like so :- Fri Oct 31 11:30:58 GMT+05:30 2014 I want to Convert it into 2014-10-31T6:00:00 which should be after adding the offset. How can I do it?

Upvotes: 0

Views: 702

Answers (3)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79115

I recommend you do it using the modern date-time API* with the following steps:

  1. Since the given date-time string has a timezone offset value(+05:30), parse it into an OffsetDateTime object using a DateTimeFormatter object created with the applicable pattern.
  2. Convert the obtained OffsetDateTime object into an OffsetDateTime object with ZoneOffset.UTC ensuring that the result is at the same instant. You can do it using OffsetDateTime#withOffsetSameInstant.
  3. The default implementation of OffsetDateTime#toString omits the second and fraction-of-second if they are zero. You can format the OffsetDateTime object, obtained in the last step, using a DateTimeFormatter object created with the applicable pattern.
  4. Your expected output also suggests that you want to ignore the seconds part. If yes, you can do so by using OffsetDateTime#truncatedTo.

Demo:

import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        String strDateTime = "Fri Oct 31 11:30:58 GMT+05:30 2014";

        DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("E MMM d H:m:s O u", Locale.ENGLISH);

        OffsetDateTime odt = OffsetDateTime.parse(strDateTime, dtfInput);

        OffsetDateTime odtUtc = odt.withOffsetSameInstant(ZoneOffset.UTC);

        DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss", Locale.ENGLISH);
        String output = odtUtc.format(dtfOutput);
        System.out.println(output);

        // In case you want to ignore the seconds
        OffsetDateTime odtUtcTruncated = odtUtc.truncatedTo(ChronoUnit.MINUTES);
        output = odtUtcTruncated.format(dtfOutput);
        System.out.println(output);
    }
}

Output:

2014-10-31T06:00:58
2014-10-31T06:00:00

Learn more about the the modern date-time API* from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Upvotes: 1

Tobías
Tobías

Reputation: 6297

First you need a SimpleDateFormat with the pattern that matches your input String: "EEE MMM dd HH:mm:ss z yyyy". Take a look at: SimpleDateFromat API

    SimpleDateFormat in = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");

Then you can parse the input String to get a corresponding Date object as follows:

    Date date = in.parse("Fri Oct 31 11:30:58 GMT+05:30 2014");

Note that Date objects does not have timezone as part of its state. If you want to print the Date in UTC then you need another SimpleDateFormat to format and print the date in your required timezone.

    SimpleDateFormat out = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    out.setTimeZone(TimeZone.getTimeZone("UTC"));
    out.format(date);   

Example: http://ideone.com/Wojec3

public static void main (String[] args) throws java.lang.Exception
{
    SimpleDateFormat in = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
    SimpleDateFormat out = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    out.setTimeZone(TimeZone.getTimeZone("UTC"));

    Date date = in.parse("Fri Oct 31 11:30:58 GMT+05:30 2014");

    System.out.println(out.format(date));
}

Upvotes: 0

Sahil Nagpal
Sahil Nagpal

Reputation: 531

This should do the task, i guess.

public static void main(String args[]) {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    format.setTimeZone(TimeZone.getTimeZone("UTC"));
    System.out.println(format.format(new Date()));  
}

Upvotes: 0

Related Questions