-1

I have a date value: - 2019-09-30T00:00:00.000+05:30

I want to convert into the format - dd MMM yyyy

I have the code:

public static String parseDateAndTimeStringCust(String datestring) {
        if(datestring.length()>=10){
            SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                String fDate = new SimpleDateFormat("dd MMM yyyy").format(dateFormat2.parse(datestring));
                return fDate;
            } catch (ParseException ex) {
                ex.printStackTrace();
            }
        }
        return datestring;
    }

Analysis: My dateFormat2 I am using is wrong

Question:

  1. What is the correct data format i have to use so that I can get the result in the format dd MMM yyyy
  2. What is the proper reference I can use in future to build such date formats
Dulaj Kulathunga
  • 1,248
  • 2
  • 9
  • 19
Devrath
  • 42,072
  • 54
  • 195
  • 297
  • 3
    Please don't use the obsolete `SimpleDateFormat` and `Date` classes. Use the newer Java Date & Time API from the `java.time` package instead. – MC Emperor Sep 19 '19 at 14:16
  • "My dateFormat2 I am using is wrong" - what exactly is the problem you're facing? Do you have to use `SimpleDateFormat` (in that case use the Javadoc on that class as a reference) or would switching to the new date api be an option (recommended)? – Thomas Sep 19 '19 at 14:17
  • 3
    "2019-09-30T00:00:00.000+05:30" - have a look at that date: there is no space between the date and the time but a `T`, it contains a millisecond part and it contains a timezone offset. So the format you're using to parse that should reflect that, e.g. something like `yyyy-MM-dd'T'HH:mm:ss.SSSXXX` – Thomas Sep 19 '19 at 14:20

3 Answers3

6

You should use classes from the java.time package.

This is what you're looking for.

String str = "2019-09-30T00:00:00.000+05:30";
OffsetDateTime dt = OffsetDateTime.parse(str);
String out = dt.format(DateTimeFormatter.ofPattern("dd MMM yyyy"));
System.out.println(out);

Note that the actual output depends on your locale, but then you could supply the Locale as the second argument to DateTimeFormatter.ofPattern.

Also note that the single-argument version of OffsetDateTime.parse implies the ISO_OFFSET_DATE_TIME format.

Furthermore, whether the actual output of abovementioned method is accurate, depends on how you want to handle the timezone offset, i.e. whether you want to convert it to UTC or ignore it. For instance, the timestamp 2019-09-30T00:00:00.000+05:30 actually falls on the 29th of September if converted to UTC (the time is then 2019-09-29T18:30:00.000Z).

DateTimeFormatter

The documentation of the DateTimeFormatter class contains detailed information about the formatting symbols and how they are parsed. There are many predefined formats for well-known and commonly used patterns, for example ISO_OFFSET_DATE_TIME conforming with the ISO 8601 standard.

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
0
public static String parseDateAndTimeStringCust(String datestring) {
        Date dataFormated = null;
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
            try {
                dataFormated = dateFormat.parse(datestring);
            } catch (ParseException ex) {
                ex.printStackTrace();
            }
        return dataFormated.toString();
}
  • 1
    These terrible classes were supplanted years ago by the *java.time* classes defined in JSR 310. Suggesting their use in 2019 is poor advice. See modern solution in Answer by MC Emperor. – Basil Bourque Sep 19 '19 at 15:56
0

Try changing following line:

SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

To:

SimpleDateFormat dateFormat2 = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
Draken
  • 3,134
  • 13
  • 34
  • 54
Prathm
  • 1
  • 1
  • 1
    These terrible classes were supplanted years ago by the *java.time* classes defined in JSR 310. Suggesting their use in 2019 is poor advice. – Basil Bourque Sep 19 '19 at 15:55