-1

how can I parse this DateTime format?

Wed Feb 03 2021 08:40:44 GMT+08:00

to

Wed 03 Feb 2021 08:40:44 am

Saeed Noshadi
  • 829
  • 12
  • 34
  • Where is your search? [I downvoted because research must be done to ask a good question](http://idownvotedbecau.se/noresearch/). – Ole V.V. May 04 '21 at 18:43

2 Answers2

3

java.time

I recommend you do it using the the modern date-time API*. The legacy date-time API (java.util date-time types and their formatting API, SimpleDateFormat) are outdated and error-prone. It is recommended to stop using them completely and switch to java.time, the modern date-time API.

Solution using modern date-time API:

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String args[]) {
        String dateStr = "Wed Feb 03 2021 08:40:44 GMT+08:00";

        DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("EEE MMM d u H:m:s O", Locale.ENGLISH);
        ZonedDateTime zdt = ZonedDateTime.parse(dateStr, dtfInput);

        DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("EEE dd MMM uuuu hh:mm:ss a", Locale.UK);
        String formatted = dtfOutput.format(zdt);
        System.out.println(formatted);
    }
}

Output:

Wed 03 Feb 2021 08:40:44 am

In case you need an object of java.util.Date from this object of ZonedDateTime, you can so as follows:

Date date = Date.from(zdt.toInstant());

Learn more about the the modern date-time API* from Trail: Date Time.

Solution using the legacy API:

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

public class Main {
    public static void main(String args[]) throws ParseException {
        String dateStr = "Wed Feb 03 2021 08:40:44 GMT+08:00";

        SimpleDateFormat sdfInput = new SimpleDateFormat("EEE MMM d y H:m:s z", Locale.ENGLISH);
        Date date = sdfInput.parse(dateStr);

        SimpleDateFormat sdfOutput = new SimpleDateFormat("EEE dd MMM yyyy hh:mm:ss a", Locale.UK);
        String formatted = sdfOutput.format(date);
        System.out.println(formatted);
    }
}

Output:

Wed 03 Feb 2021 12:40:44 am

* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • 1
    A modest suggestion, since your parsing will accept any GMT offset, either convert the time to the time zone appropriate for the user or at least check that the parsed offset was +08:00 as expected. – Ole V.V. May 04 '21 at 18:42
0

Have you taken a look at the SimpleDateFormat class (https://developer.android.com/reference/java/text/SimpleDateFormat)? It should be able to display the date any way you like.

Edit: Please see Arvind Kumar Avinash's answer instead.

JacquesBauer
  • 226
  • 2
  • 7
  • Yes. I saw. But I have not found a format from which GMT+08:00 can be removed. – Saeed Noshadi May 04 '21 at 15:08
  • I think the string you are looking for looks like such: "EEE dd MMM yyyy hh:mm:ss aa" . Therefore, you can make a SimpleDateFormat like: `SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE dd MMM yyyy hh:mm:ss aa");` – JacquesBauer May 04 '21 at 15:33
  • 1
    Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. We have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android look into [desugaring](https://developer.android.com/studio/write/java8-support-table) or see [How to use ThreeTenABP …](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. May 04 '21 at 18:35
  • 1
    @OleV.V. Oh ok I was not aware when I posted this. I believe arvind-kumar-avinash 's answer is more appropriate then. – JacquesBauer May 04 '21 at 19:25