4
  private void parseDate() {
        String p_localDateTime = "Mon Aug 08 16:44:19 EAT 2016";
        SimpleDateFormat lv_formatter,lv_parser;
        String lv_localTimeZone ="";
        lv_localTimeZone="EAT";
        Date lv_localDate = null;

        lv_parser = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
        //lv_parser = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
        //lv_parser = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy");
        lv_parser.setTimeZone(TimeZone.getTimeZone(lv_localTimeZone));
        try {
            lv_localDate = lv_parser.parse(p_localDateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println("convertLocalTimeToUTC: ");
    }

I have tried to parse this date using 3 differnt zone formats 'z','zzz','zzzz' but all throws java.text.ParseException: Unparseable date: “Mon Aug 08 16:44:19 EAT 2016” (at offset 20)

Krishna Upadhya
  • 131
  • 2
  • 10
  • *FYI:* `EAT` is East Africa Time. – Andreas Aug 23 '16 at 05:53
  • Thanks everyone for the answers, I am able to parse date using above functions using java compilers, but issue is occurring when I try to use same method in android studio. In android its throwing the exception but in java compiler it works fine – Krishna Upadhya Aug 23 '16 at 07:22
  • @CoderAndroid Post further info as edits to the Question rather than as comments. – Basil Bourque Aug 23 '16 at 15:04

5 Answers5

4

You have to add the locale:

lv_parser = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy",Locale.ENGLISH);

Full Example:

      String p_localDateTime = "Mon Aug 08 16:44:19 EAT 2016";
        SimpleDateFormat lv_formatter,lv_parser;
        String lv_localTimeZone ="";
        lv_localTimeZone="EAT";
        Date lv_localDate = null;

        lv_parser = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy",Locale.ENGLISH);          //lv_parser = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
        //lv_parser = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy");
        lv_parser.setTimeZone(TimeZone.getTimeZone(lv_localTimeZone));
        try {
            lv_localDate = lv_parser.parse(p_localDateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println("convertLocalTimeToUTC: ");
Jens
  • 67,715
  • 15
  • 98
  • 113
  • Thanks Jens for reply, but Still its throwing same exception after adding Locale.ENGLISH, can you check once if there is anything else i need to do – Krishna Upadhya Aug 23 '16 at 05:50
  • @CoderAndroid I have tried it by my own. It works fine – Jens Aug 23 '16 at 05:52
  • Can you please post complete code that you have tried. It will be helpful to solve my issue. – Krishna Upadhya Aug 23 '16 at 05:54
  • @CoderAndroid Have added at to my answer. As you can see, i have copied your example and added only the Locale. – Jens Aug 23 '16 at 05:56
  • Thanks for the answer, I am able to parse date using above functions using java compilers, but issue is occurring when I try to use same method in android studio. In android its throwing the exception but in java compiler it works fine – Krishna Upadhya Aug 23 '16 at 07:23
1

As mentioned in below link it appears to be Timezone doesnot accept "ZZZ" format according SimpleDateFormat documentation. link is: Simpledateformat ParseException

I was able to solve this by mentioning zone as "East Africa Time" instead of "EAT" and code is:

 public void  dateFormatter() {
  String dateString = "Mon Aug 23 8:42:19 East Africa Time 2016";
  String dateFormat= "EEE MMM dd HH:mm:ss z yyyy";
        SimpleDateFormat setDateFormatter;
        Date formattedDate = null;
        if(!dateString.isEmpty()) {
            setDateFormatter = new SimpleDateFormat(dateFormat, Locale.ENGLISH);
            try {
                formattedDate = setDateFormatter.parse(dateString);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        System.out.println("Formatted Date: " + formattedDate);

    }
Community
  • 1
  • 1
Krishna Upadhya
  • 131
  • 2
  • 10
0

No error from my side for the code. I change a bit try to output the result and get this

"convertLocalTimeToUTC: Mon Aug 08 21:44:19 SGT 2016"

package self.test.out;

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

public class DateParseTest {

    public static void main(String[] args) {
        String p_localDateTime = "Mon Aug 08 16:44:19 EAT 2016";
        SimpleDateFormat lv_formatter,lv_parser;
        String lv_localTimeZone ="";
        lv_localTimeZone="EAT";
        Date lv_localDate = null;

        lv_parser = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
        //lv_parser = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
        //lv_parser = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy");
        lv_parser.setTimeZone(TimeZone.getTimeZone(lv_localTimeZone));
        try {
            lv_localDate = lv_parser.parse(p_localDateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println("convertLocalTimeToUTC: "+lv_localDate);
    }
}

using Jdk1.6

ommar
  • 101
  • 1
  • 8
  • Thanks for the answer, I am able to parse date using above functions using java compilers, but issue is occurring when I try to use same method in android studio. In android its throwing the exception but in java compiler it works fine – Krishna Upadhya Aug 23 '16 at 07:23
0

Here is a utility function that I use in my projects. Just pass the string and the format of the date. In this case the dateformat would be "EEE MMM dd HH:mm:ss Z yyyy". Hope its helpful.

public static Date dateFormatter(String dateString, String dateFormat) {

    SimpleDateFormat setDateFormatter = new SimpleDateFormat(dateFormat, Locale.ENGLISH);
    Date formattedDate = null;
    if (!dateString.isEmpty()) {
        try {
            formattedDate = setDateFormatter.parse(dateString);
        } catch (ParseException e) {
            Log.e(TAG, "Parse Exception Error : " + e.getMessage());
        }
    }

    return formattedDate;
}
Gaurav Sarma
  • 2,248
  • 2
  • 24
  • 45
  • Can you pl. check once if you can parse the date - "Mon Aug 08 16:44:19 EAT 2016". I am getting parse exception in studio, if I user other java compilers it works fine. It will parse "Mon Aug 08 16:44:19 **IST** 2016" but not for "Mon Aug 08 16:44:19 **EAT** 2016". – Krishna Upadhya Aug 23 '16 at 07:34
  • try using capital Z once like this "EEE MMM dd HH:mm:ss Z yyyy" and let me know. – Gaurav Sarma Aug 23 '16 at 07:39
  • It is working for the devices below lollipop, but not parsing in devices above lollipop. – Krishna Upadhya Aug 23 '16 at 08:10
  • So its now parsing "Mon Aug 08 16:44:19 **EAT** 2016" correctly after using the Z but only on pre lolipop devices is that correct ? – Gaurav Sarma Aug 23 '16 at 08:15
  • Even if we use small 'z' it will parse in pre lollipop devices. – Krishna Upadhya Aug 23 '16 at 08:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/121597/discussion-between-coder-android-and-gaurav4sarma). – Krishna Upadhya Aug 23 '16 at 08:31
0

You are using troublesome old date-time classes now supplanted by the java.time classes.

Using java.time

Avoid using EAT as a zone. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!). Specify a proper time zone name. In this particular case, java.time is able to translate that as Africa/Nairobi zone.

String input = "Mon Aug 08 16:44:19 EAT 2016";
DateTimeFormatter f = DateTimeFormatter.ofPattern ( "EEE MMM dd HH:mm:ss z uuuu" ).withLocale ( Locale.US );
ZonedDateTime zdt = ZonedDateTime.parse ( input , f );

Dump to console. The toString method generates a String in standard ISO 8601 format, extended by appending the name of the zone in brackets. These standard formats are a much better choice for when you need to serialize date-time values to text.

System.out.println ( "zdt: " + zdt );

zdt: 2016-08-08T16:44:19+03:00[Africa/Nairobi]

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the old troublesome date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to java.time.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154