1

Im trying parse a String with a date to convert it into a Date format. Strings are in the following format.

Thursday, Jan 09 2020; 04:31:59 PM (GMT +05:30)

SimpleDateFormat sdf3 = new SimpleDateFormat("EEE, MMM dd yyyy; hh:mm:ss a",Locale.ENGLISH);

for(int i=0 ; i <jArr.length() ; i++){
    String tempDate = jArr.get(i).toString();
    dateList.add(tempDate);
}

try{
    Date d1 = sdf3.parse(dateList.get(0));                        
} catch (Exception e) {
    e.printStackTrace(); 
}

Note: This function is working fine for android version > 6.

Vishal Patoliya ツ
  • 3,170
  • 4
  • 24
  • 45
  • 1
    Have you tried with `sdf3.parse("Thursday, Jan 09 2020; 04:31:59 PM (GMT +05:30)");`? It's working fine in my android 4.4.4 – Md. Asaduzzaman Jan 09 '20 at 12:11
  • I have tried but it is actually returning date with addition of +5:30 which is not expected, is there any other format which is returning without +5:30 ? @Md.Asaduzzaman – Vishal Patoliya ツ Jan 09 '20 at 12:23
  • Actually what do you want? – Md. Asaduzzaman Jan 09 '20 at 12:24
  • I just want to convert date from Thursday, Jan 09 2020; 04:31:59 PM (GMT +05:30) to 09/01/20; 04:31:59PM @Md.Asaduzzaman – Vishal Patoliya ツ Jan 09 '20 at 12:27
  • 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. Jan 10 '20 at 00:23
  • You need to parse the GMT offset from the string too to avoid getting that false time. – Ole V.V. Jan 10 '20 at 00:26

3 Answers3

2

To get your desired format you have to use SimpleDateFormat like below:

String dateString = "Thursday, Jan 09 2020; 04:31:59 PM (GMT +05:30)";
SimpleDateFormat sourceFormat = new SimpleDateFormat("EEE, MMM dd yyyy; hh:mm:ss a",Locale.ENGLISH);

try{
    Date d1 = sourceFormat.parse(dateString);                        
} catch (Exception e) {
    e.printStackTrace(); 
}

SimpleDateFormat targetFormat = new SimpleDateFormat("dd/MM/yy; hh:mm:ss a", Locale.ENGLISH)

String desiredString = targetFormat.format(d1);
//desiredString is now "09/01/20; 04:31:59 PM"
Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46
0

Try this EPOCH_FORMAT in the arguments of SimpleDateFormat :

String formatter = "EE MMM dd HH:mm:ss z yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(formatter,Locale.US);
Kalpesh Rupani
  • 991
  • 4
  • 12
0

java.time and ThreeTenABP

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
            "EEEE, MMM dd uuuu; hh:mm:ss a '(GMT' xxx')'", Locale.ENGLISH);

    String tempDate = "Thursday, Jan 09 2020; 04:31:59 PM (GMT +05:30)";
    OffsetDateTime odt = OffsetDateTime.parse(tempDate, formatter);
    System.out.println(odt);

Output from this snippet is:

2020-01-09T16:31:59+05:30

In order to get the correct time you need to parse the GMT offset that is in the string.

I am using java.time, the modern Java date and time API because SimpleDateFormat and Date are poorly designed and long outdated, the former in particular notoriously troublesome. And because java.time is so much nicer to work with.

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