1

I am parsing date of string type into Date format , but everytime got this exception

java.text.ParseException: Unparseable date: "2016-05-21T00:00:00" (at offset 4)

My code is :

   String d = "2016-05-21T00:00:00";
    DateFormat df = new SimpleDateFormat("yyyy MM dd HH:mm:ss", Locale.ENGLISH);
    Date myDate = null;
    try {
        myDate = df.parse(d);
    } catch (ParseException e) {
        e.printStackTrace();
    }
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Sandeep Singh
  • 1,013
  • 1
  • 11
  • 25
  • 1
    check [link](http://stackoverflow.com/questions/15730298/java-format-yyyy-mm-ddthhmmss-sssz-to-yyyy-mm-dd-hhmmss) – Manish Jain Jun 06 '16 at 05:00
  • Your `SimpleDateFormat` that you're using to parse doesn't understand your input string - the formats don't match. Simply fix it so your parser matches the input format. As a note, your input string format is almost [ISO-8601](https://en.wikipedia.org/wiki/ISO_8601) format – Krease Jun 06 '16 at 05:01
  • i am wondering... why is it *almost* the same? Can the input be changed so it *is* the same? There's no value in *almost* using a standard... – Krease Jun 06 '16 at 05:19

5 Answers5

8

Your date string and DateFormat need to match. For your input "2016-05-21T00:00:00", the correct DateFormat is:

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
1615903
  • 32,635
  • 12
  • 70
  • 99
3

tl;dr

LocalDateTime.parse( "2016-05-21T00:00:00" )

ISO 8601

The input string 2016-05-21T00:00:00 is in standard ISO 8601 format.

java.time

The java.time classes built into Java 8 and later use the ISO 8601 formats by default when parsing or generating textual representations of date-time values.

The input string lacks any offset-from-UTC or time zone info. So the string by itself is imprecise, has no specific meaning as it is not a moment on the timeline. Such values are represented in java.time by the LocalDateTime class.

String input = "2016-05-21T00:00:00";
LocalDateTime ldt = LocalDateTime.parse( input );

If you know from a larger context that the string is intended to have meaning for a certain offset-from-UTC, assign a ZoneOffset to get a OffsetDateTime object.

ZoneOffset zoneOffset = ZoneOffset.of( 5 , 30 );
OffsetDateTime odt = ldt.atOffset( zoneOffet );

Even better, if you are certain of an intended time zone, specify a ZoneId to get a ZonedDateTime.

ZoneId zoneId = ZoneId.of( "Asia/Kolkata" );
ZonedDateTime zdt = ldt.atZone( ZoneId );

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Using a JDBC driver compliant with JDBC 4.2 or later, you may exchange java.time objects directly with your database. No need for strings nor java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0
String expectedDateFormat=getDateFromString("2016-05-21 00:00:00","yyyy-MM-dd HH:mm:ss","yyyy MM dd HH:mm:ss");

This method will perform actual format to expected format.

public static String getDateFromString(String dateInString, String actualformat, String exceptedFormat) {
        SimpleDateFormat form = new SimpleDateFormat(actualformat);

        String formatedDate = null;
        Date date;
        try {
            date = form.parse(dateInString);
            SimpleDateFormat postFormater = new SimpleDateFormat(exceptedFormat);
            formatedDate = postFormater.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return formatedDate;
    }
Sackurise
  • 2,804
  • 1
  • 18
  • 18
-1
public  class test {

    public static void main(String [] args) {

        // ref http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
           SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd hh:mm:ss");
          System.out.println("Current Date: " + simpleDateFormat.format(new Date()));
    }

}

for more information on SimpleDateFormat

Prasad
  • 616
  • 7
  • 11
-3

try this way

String d = "2016-05-21 00:00:00";
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
    Date myDate = null;
    try {
        myDate = df.parse(d);
    } catch (ParseException e) {
        e.printStackTrace();
    }

EDIT

String d = "2016-05-21 00:00:00";
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
    Date myDate = null;
    try {
        myDate = df.parse(d);
    } catch (ParseException e) {
        e.printStackTrace();
    }
Harshal Kalavadiya
  • 2,412
  • 4
  • 38
  • 71