How to parse the string "Mon Aug 08 16:44:19 EAT 2016" today in android

  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 tried to parse this date using 3 different zone formats "z", "zzz", "zzzz", but all throws java.text.ParseException: Unparseable date: "Mon Aug 08 16:44:19 EAT 2016" (at offset twenty)

+4
source share
5 answers

You are using the nasty old time classes that are now superseded by java.time classes.

Using java.time

EAT . 3-4 , EST IST, , (!). . java.time Africa/Nairobi .

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 );

. toString ISO 8601, . - , .

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

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

java.time

java.time Java 8 . , java.util.Date, .Calendar java.text.SimpleDateFormat.

Joda-Time, , java.time.

, . Oracle. Qaru .

java.time Java 6 7 ThreeTen-Backport Android ThreeTenABP (. ...).

ThreeTen-Extra java.time . java.time. , Interval, YearWeek, YearQuarter ..

0

:

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

:

      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: ");
+4

, , Timezone ZZZ SimpleDateFormat. : Simpledateformat ParseException

, "- " "EAT", :

 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);

    }
+1

.

"convertLocalTimeToUTC: 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);
    }
}

Jdk1.6

0

Here is the utility that I use in my projects. Just pass the string and date format. In this case, the date format is "EEE MMM dd HH: mm: ss Z yyyy". Hope this will be 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;
}
0
source

Source: https://habr.com/ru/post/1652135/


All Articles