-1

I am receiving email arrival Date like "Tue,14 Jul 2020 03: 15: 03 +0000 (UTC)", this needs to converted into this format "2020-02-11 16:05:00". Can anyone please help me to achieve this Date conversion?

Partially formed Input Date format like : EEE, d MMM yyyy HH:mm:ss

Can anyone give me exact date format for my input Date?

What i have tried:

try
     {
        String date_s = "Tue,14 Jul 2020 03: 15: 03 +0000 (UTC)";
        SimpleDateFormat simpledateformat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss");
        Date tempDate=simpledateformat.parse(date_s);
        SimpleDateFormat outputDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");           
        System.out.println("Output date is = "+outputDateFormat.format(tempDate));
      } catch (Exception ex) 
      {

          ex.printStackTrace();
      }

Exception like below:

java.text.ParseException: Unparseable date: "Tue,14 Jul 2020 03: 15: 03 +0000 (UTC)"
    at java.text.DateFormat.parse(DateFormat.java:366)
    at JavaPackage.DateConvertion.main(DateConvertion.java:12)

Awaiting for your response.

Note: Just for Date Format Identification purpose , randomly given above converted Date.

Justin
  • 855
  • 2
  • 11
  • 30
  • 1
    Does this answer your question? [Change date format in a Java string](https://stackoverflow.com/questions/4772425/change-date-format-in-a-java-string) – OH GOD SPIDERS Nov 12 '20 at 15:48
  • Hi @OHGODSPIDERS, Thanks for useful post, it gives lot of information , but unfortunately i can't able to find desired pattern for my use case. Can u pls help me into this? – Justin Nov 12 '20 at 16:03
  • 1
    Please edit the question to show how far you've got then, and exactly which part of the pattern is causing problems for you. – Jon Skeet Nov 12 '20 at 16:09
  • Hi @JonSkeet, As per your suggestion, added code snippet, kindly check this – Justin Nov 12 '20 at 16:21
  • Okay, so look at the pattern you've specified and the input - do they look the same? Hint: they start going wrong where your pattern expects a space after the comma. But that's far from all that's wrong. You also probably want to specify a `Locale` otherwise it will try to use your system default locale for the month names. I'd also strongly advise using java.time instead of SimpleDateFormat. – Jon Skeet Nov 12 '20 at 16:23
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `OffsetDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Nov 12 '20 at 16:23
  • Hi @JonSkeet, I tried with pattern like "EEE,dd MMM yyyy HH: mm: ss" which gives output as "2020-07-14 03:15:03". as per suggestion u guys gave , i will try to use modern Java Date and Time APIs. – Justin Nov 12 '20 at 16:37
  • Hi @OleV.V. Thanks for sharing useful information. will follow modern java date and time APIs – Justin Nov 12 '20 at 16:38

2 Answers2

2

The date-time API of java.util and their formatting API, SimpleDateFormat are outdated and error-prone. I suggest you should stop using them completely and switch to the modern date-time API.

Using the modern date-time API:

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

public class Main {
    public static void main(String[] args) {
        String strDateTime = "Tue,14 Jul 2020 03: 15: 03 +0000 (UTC)";
        DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("EEE,d MMM uuuu H: m: s Z '('z')'", Locale.ENGLISH);
        OffsetDateTime odt = OffsetDateTime.parse(strDateTime, dtfInput);

        DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss", Locale.ENGLISH);
        System.out.println(dtfOutput.format(odt));
    }
}

Output:

2020-07-14 03:15:03

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

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.

Using legacy API:

import java.text.DateFormat;
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 strDateTime = "Tue,14 Jul 2020 03: 15: 03 +0000 (UTC)";
        DateFormat sdfInput = new SimpleDateFormat("EEE,d MMM yyyy H: m: s Z '('z')'", Locale.ENGLISH);
        Date date = sdfInput.parse(strDateTime);

        DateFormat sdfOutput = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
        sdfOutput.setTimeZone(sdfInput.getTimeZone());
        System.out.println(sdfOutput.format(date));
    }
}

Output:

2020-07-14 03:15:03
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • Hi @Arvind Kumar Avinash, Thank you so much for your answer by using java.time packages. I am working in Integration(WSO2) Project, In this i can use Class mediator in which able to write java code. so i use java.time package and not getting date pattern u used, can u pls explain date format that you are using in above code? – Justin Nov 12 '20 at 16:46
  • @Justin - The pattern `uuuu` can be used only with the modern date-time API. You can use `yyyy` instead of `uuuu`. The symbol, `u` specifies `year` whereas `y` specifies `year-of-era`. Check [this](https://stackoverflow.com/questions/29014225/what-is-the-difference-between-year-and-year-of-era) to understand the difference. Feel free to comment in case of any further doubt. – Arvind Kumar Avinash Nov 12 '20 at 17:40
1

The presence of spaces in your string looks funny. If the spaces always occur in those places, use the answer by Arvind Kumar Avinash. If there are variations in the occurrence of spaces, you can handle them by enclosing the spaces in square brackets in the format pattern string, thus:

    DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern(
            "EEE,[ ]d MMM uuuu H[ ]:[ ]mm[ ]:[ ]ss xx[ ]'('z')'", Locale.ENGLISH);

    String strDateTime = "Tue,14 Jul 2020 03: 15: 03 +0000 (UTC)";
    OffsetDateTime odt = OffsetDateTime.parse(strDateTime, dtfInput);
    
    System.out.println(odt);

Output:

2020-07-14T03:15:03Z

Square brackets in the format pattern string enclose optional parts, so the formatter above will parse strings that have or don’t have each of those spaces.

Link

Oracle tutorial: Date Time explaining how to use java.time.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Hi @Ole V.V. Yes you are right.there is variation in Spaces. so i used the pattern which u shared in above answer. Again thanks a lot – Justin Nov 12 '20 at 17:21
  • Hi @Ole V.V Sometimes i received email Date in another format like "Mon, 7 Sep 2020 16:54:33 +0530" which is varied from previous one "Tue,14 Jul 2020 03: 15: 03 +0000 (UTC)". It gives error like "java.time.format.DateTimeParseException: Text 'Mon, 7 Sep 2020 16:54:33 +0530' could not be parsed at index 30" So how can we manage these in the same pattern?or else do we have some other options?kindly let me know – Justin Nov 17 '20 at 09:56
  • 1
    Try putting that *(UTC)* in square brackets too: `EEE,[ ]d MMM uuuu H[ ]:[ ]mm[ ]:[ ]ss xx[[ ]'('z')']`. – Ole V.V. Nov 17 '20 at 10:15