0

I'm trying to parse below string into date but its showing wrong date

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

public class Dateformat {
    public static void main(String[] args) throws Exception {
        String sDate1 = "2018-10-05T00:00:00-05:00";
        Date date1 = new SimpleDateFormat("yyyy-mm-dd").parse(sDate1);
        System.out.println(sDate1 + "\t" + date1);
    }
}

Doubt: why even its working if pattern is not matching with input string ?

Secondly, why its showing wrong date ?

Output : 2018-10-05T00:00:00-05:00  Fri Jan 05 00:10:00 IST 2018

Suggestions please

Elena
  • 181
  • 1
  • 11
  • As far as the first question: by default the parser is `lenient`: If no exact match is found, it tries to parse anyway and in your case succeeds. – Robert Kock Oct 12 '18 at 08:07
  • SimpleDateFormat("yyyy-mm-dd") yyy-mm-dd here is for input date format, not for output. – Axbor Axrorov Oct 12 '18 at 08:07
  • @AxborAxrorov She is complaining about wrong Date not wrong format – Scary Wombat Oct 12 '18 at 08:08
  • import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class SimpleDateFormatExample3 { public static void main(String[] args) { SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); try { Date date = formatter.parse("31/03/2015"); System.out.println("Date is: "+date); } catch (ParseException e) {e.printStackTrace();} } } output is: Date is: Tue Mar 31 00:00:00 IST 2015 – Ganesh Chowdhary Sadanala Oct 12 '18 at 08:09
  • @ScaryWombat and what am I about? She must choose right format to parse the date. – Axbor Axrorov Oct 12 '18 at 08:10
  • What is your expected output ? – Sudhir Ojha Oct 12 '18 at 08:12
  • @SudhirOjha i have mentioned the output and that is the actual format – Ganesh Chowdhary Sadanala Oct 12 '18 at 08:13
  • The format you should use is: `"yyyy-MM-dd"` So: MM -> month, mm -> minute – Adly Oct 12 '18 at 08:14
  • @Adly i have already mentioned the correct format and gave a sample program i.e; mm is for minute and MM is for month – Ganesh Chowdhary Sadanala Oct 12 '18 at 08:17
  • 1
    Use modern *java.time* classes: `OffsetDateTime.parse( "2018-10-05T00:00:00-05:00" ).toLocalDate().toString()` – Basil Bourque Oct 12 '18 at 14:47
  • @RobertKock `SimpleDateFormat` is more complicated than that. While it’s true what you say, leniency is not relevant here. Setting the format non-lenient still gave `Fri Jan 05 00:10:00 CET 2018` on my computer. – Ole V.V. Oct 14 '18 at 04:36
  • Elena, I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Oct 14 '18 at 04:38

5 Answers5

5

m is for Minute in hour

try

String sDate1 = "2018-10-05T00:00:00-05:00";
Date date1 = new SimpleDateFormat("yyyy-MM-dd").parse(sDate1);
System.out.println(sDate1 + "\t" + date1);

output

2018-10-05T00:00:00-05:00 Fri Oct 05 00:00:00 JST 2018

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
2

I don't know why you are using Date? It is deprecated and not useful since java 8. It is better to use LocalDateTime, LocalDate or LocalTime now.(Update base on comment of user https://stackoverflow.com/users/5772882/ole-v-v)

        String date = "2018-10-05T00:00:00-05:00";
        OffsetDateTime result = OffsetDateTime.parse(date);
        LocalDate localDate = LocalDate.from(result);
        System.out.println(localDate);
Slava Lubarskyi
  • 118
  • 1
  • 11
  • 1
    Since the string has a UTC offset and no time zone, the best class to use here is `OffsetDateTime`. When you do, you also don’t need to specify a formatter since the string is in the default (ISO 8601) format for `OffsetDateTime`. – Ole V.V. Oct 12 '18 at 08:30
  • The comment by Ole V.V. is correct: The `OffsetDateTime` is more appropriate here than `ZonedDateTime`. – Basil Bourque Oct 12 '18 at 19:09
1

The format you should use is: "yyyy-MM-dd"

  • MM > month
  • mm > minute
Adly
  • 597
  • 3
  • 12
  • 23
0

Cutting off the rest of the string which is not needed is one way to do it. Keep the substring of the part you need and the Date should be working fine.

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

public class Dateformat {
    public static void main(String[] args) throws Exception {
        String sDate1 = "2018-10-05T00:00:00-05:00";
        String formatedDate = sDate1.substring(0,10);
        Date date1 = new SimpleDateFormat("yyyy-MM-dd").parse(formatedDate);
        System.out.println(sDate1 + "\t" + date1);
    }
}

FormatedDate is: 2018-10-05

The output is: 2018-10-05T00:00:00-05:00 - Fri Oct 05 00:00:00 UTC 2018

Sir. Hedgehog
  • 1,260
  • 3
  • 17
  • 40
0

You are giving wrong format to parse the date! new SimpleDateFormat("yyyy-mm-dd") yyyy-mm-dd here is format for input (which you are trying to parse). you must use format yyyy-MM-dd .

for formatting output you must write code like:

class DateUtils {
    private static SimpleDateFormat fromFormat = new SimpleDateFormat("yyyy-MM-dd", Locale("ru","RU"));
    private static SimpleDateFormat toFormat = new SimpleDateFormat("dd MMM yyyy", Locale("ru","RU"));

    public static String formatDate(String date) {
        return toFormat.format(fromFormat.parse(date));
    }
}

example of using:

DateUtils.formatDate("2018-10-05T00:00:00-05:00");

and the result is:

05 OCT 2018
Axbor Axrorov
  • 2,720
  • 2
  • 17
  • 35