-2

How to parse the following timestamp in string formate to date formate.

Mon Sep 25 13:40:56 GMT+05:30 2017

Since it has GMT in the timestamp so I didn't find a valid answer for this. Please let me know how to write SimpleDateFormat for this?

Seenu69
  • 1,041
  • 2
  • 15
  • 33
  • with SimpleDateFormat – François LEPORCQ Oct 05 '17 at 11:36
  • 2
    Did you google your title ? – AxelH Oct 05 '17 at 11:38
  • I upvoted this question because is not so evident with the GMT part – François LEPORCQ Oct 05 '17 at 11:43
  • 1
    @FrançoisLEPORCQ I don't think so... the [`SimpleDateFormat`](https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html) is complet and provide every supported date/time component with example. – AxelH Oct 05 '17 at 12:07
  • @AxelH Well please write the answer with SimpleDateFormat if you know. – Seenu69 Oct 05 '17 at 12:10
  • 2
    My answer is my comment, the documentation is enought for you to do some research. We are not here to provide a ready to use solution. – AxelH Oct 05 '17 at 12:11
  • I did a research, but I didn't get a valid solution hence I have put my problem on stackoverflow – Seenu69 Oct 05 '17 at 12:14
  • 1
    In the [duplicate target](https://stackoverflow.com/q/4216745/7605325) there's [an answer](https://stackoverflow.com/a/4216767/7605325) with all the possible patterns (including the GMT part). Have you tried something? If so, you can [edit] the question and add the code you tried, and also explain why it's different from the other question, and what error you're getting, and so on. Otherwise, your question will be considered a duplicate. –  Oct 05 '17 at 12:18
  • 1
    @Seenu69 then you probably already tried using a `SimpleDateFormat`, you should post the code you had. This would have been a more valid question. – AxelH Oct 05 '17 at 12:41

1 Answers1

3

in java 7 with SimpleDateFormat

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {
    public static void main(String[] args) {
        String input = "Mon Sep 25 13:40:56 GMT+05:30 2017";
        String dateFormat = "EEE MMM d HH:mm:ss z yyyy";
        try {
            Date date = new SimpleDateFormat(dateFormat).parse(input);
            System.out.println(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }
}

See it in action : https://ideone.com/RaCugz

  • I have checked in Android studio, it unable to import DateTimeFormatter?? – Seenu69 Oct 05 '17 at 12:05
  • Are you in java 8 ? – François LEPORCQ Oct 05 '17 at 12:06
  • @Seenu69 because Java 8 is not supported on every Android version (since Android 7 or 8 I think) – AxelH Oct 05 '17 at 12:07
  • 1
    @Seenu69 In Android (if you're ok with adding a dependency to your project, which in this case, it's totally worth IMO) you can use the [ThreeTen Backport](http://www.threeten.org/threetenbp), a great backport for Java 8's new date/time classes. To make it work, you'll also need the [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) (more on how to use it [here](https://stackoverflow.com/a/38922755/7605325)) –  Oct 05 '17 at 12:21
  • 1
    @FrançoisLEPORCQ In Java 8, you could parse directly to `OffsetDateTime` (as the input string already contains all the information necessary), and then call `toInstant()` directly from it. Parsing to `LocalDate` discards the time and offset information, and using `atStartOfDay(ZoneId.systemDefault())` sets the time to midnight, and uses the JVM default timezone (which is not guaranteed to have the same offset as the input). –  Oct 05 '17 at 12:27
  • You're right thanks for that – François LEPORCQ Oct 05 '17 at 12:30
  • 2
    Note that this will work only if the locale is in ENGLISH. If I run this (having a FRENCH locale) even if a `Date.toString` return an english timestamp, it won't be able to parse it. I would need to set the Locale (for instance) to ENGLISH. (This is why it is risky to include textual date field). (You can set the locale using the `SimpleDateFormat(String, Locale)` constructor ;) ) – AxelH Oct 05 '17 at 12:39