-1

I am having this string "Mon Nov 11 10:36:53 GMT+02:00 2019". What is the pattern when using SimpleDateFormat();? Is there some way I can test or generate it without multiple try and error?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Dim
  • 4,527
  • 15
  • 80
  • 139
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Nov 13 '19 at 19:49
  • Possible duplicate of [how to parse output of new Date().toString()](https://stackoverflow.com/questions/4713825/how-to-parse-output-of-new-date-tostring) – Ole V.V. Nov 13 '19 at 19:50

4 Answers4

1

Try using

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46
1
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class Test{
    public static void main(String[] args) {

        //Try This

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss 'GMT' Z yyyy");

        System.out.println(simpleDateFormat.format(new Date()));
    }
}
Ayyub Kolsawala
  • 809
  • 8
  • 15
1

java.time and ThreeTenABP

What is the pattern when using SimpleDateFormat();?

My suggestion is that you don’t use SimpleDateFormat. That class is notoriously troublesome and long outdated. On Android — and on your API level too — you can use java.time, the modern Java date and time API.

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

    String stringWereHaving = "Mon Nov 11 10:36:53 GMT+02:00 2019";
    ZonedDateTime dateTime = ZonedDateTime.parse(stringWereHaving, formatter);
    System.out.println(dateTime);

Output is:

2019-11-11T10:36:53+02:00[Etc/GMT-2]

The only confusing thing here is that the sign used in the time zone name Etc/GMT-2 has been intentionally reversed compared to normal usage.

Only if you need an old-fashioned Date object for a legacy API not yet upgraded to java.time, convert like this:

    Instant i = dateTime.toInstant();
    Date oldfashionedDate = DateTimeUtils.toDate(i);
    System.out.println(oldfashionedDate);

Mon Nov 11 09:36:53 CET 2019

Output comes from my computer in Europe/Copenhagen time zone, so the hour of day is adjusted by 1 hour compared to your input at offset `02:00. We have got the same point in time as in the string.

Is there some way I can test or generate it without multiple try and error?

There’s always Java SimpleDateFormat Online Tester. I give you the link at the bottom. I don’t know of a similar service for DateTimeFormatter. Many of the patterns are the same, also the one I am using above, so it’s probably worthwhile trying.

Question: Doesn’t java.time require Android API level 26?

java.time works nicely on both older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

What do you mean " Is there some way I can test or generate it without multiple try and error?"

BTW, I'm using DateFormat dateFormat = new SimpleDateFormat(<put your desired pattern here via STRING>);

and get the current date:

 Date date = new Date();
 String result = dateFormat.format(date); // formatting the date and passing it on string.

sample code:

DateFormat dateFormat = new SimpleDateFormat("MMM. dd, yyyy EEE HH:mm:ss");
Date date = new Date();

result = dateFormat.format(date);

Output : Nov. 13, 2019 Wed 17:02:00

Rubick
  • 296
  • 3
  • 22