1

I am not able to parse a date using SimpleDateFormat. I have tried this code:

SimpleDateFormat fechas = new SimpleDateFormat("dd/MM/yyyy hh:mm");

if (data[i].length() > 1) {
    Date f = (Date) fechas.parse(data[i]);
    System.out.println(i + " " + f);
}

I get the following error:

Exception in thread "main" java.text.ParseException: Unparseable date: "01/06/2015 8:20

"

I have the same problem again with the following code:

SimpleDateFormat fech = new SimpleDateFormat(" yyyy/MM/dd HH:mm:ss");
Date date = (Date) fech.parse(data[i]);
System.out.println(date);

Which gives the error

Exception in thread "main" java.text.ParseException: Unparseable date: "00015/06/01  08:20:15"

How can I fix this problem?

Jonathan Caicedo
  • 131
  • 1
  • 2
  • 6
  • 6
    Your date strings don't match the patterns, just like the error message tells you. – Keppil Jul 20 '15 at 14:34
  • 2
    Just a guess, but... "01/06/2015" does not contain a "hh:mm" part. And the 2nd thing has a) two whitespaces in the middle and b) a year of "00015", which doesn't seam right. – Florian Schaetz Jul 20 '15 at 14:35
  • first is "00015/06/01 08:20:15" and the other "01/06/2015 8:20:10" – Jonathan Caicedo Jul 20 '15 at 15:05
  • (`HH` = 24 hours, `hh` = 12 hours range.) – Joop Eggen Jul 20 '15 at 15:33
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque May 24 '18 at 06:21

4 Answers4

2

When using SimpleDateFormat, the date format has to match exactly. In your example, you include the date, but in your date format, you also specify the hours and minutes. If your data had that text, it would work. For instance, using your first example:

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

public class DateDemo {
  public static void main(String args[]) throws Exception {
    String yourData = "01/06/2015";
    String matchingData = "01/06/2015 13:00";
    SimpleDateFormat fechas = new SimpleDateFormat("dd/MM/yyyy hh:mm");

    Date matchingDate = fechas.parse(matchingData);
    System.out.println("String: \"" + matchingData + "\" parses to " + matchingDate);
    Date yourDate = fechas.parse(yourData);
    System.out.println("String: \"" + yourData + "\" parses to " + yourDate);
  }
}

This outputs:

String: "01/06/2015 13:00" parses to Mon Jun 01 13:00:00 CDT 2015
Exception in thread "main" java.text.ParseException: Unparseable date: "01/06/2015"
        at java.text.DateFormat.parse(DateFormat.java:366)
        at Demo.main(Demo.java:14)
durron597
  • 31,968
  • 17
  • 99
  • 158
1

tl;dr

How can I fix this problem?

Match your formatting pattern to your input.

LocalDateTime.parse( 
    "01/06/2015 8:20" , 
    DateTimeFormatter.ofPattern( "dd/MM/uuuu H:m" )
)
.toString()

2015-06-01T08:20

java.time

As others stated, your formatting pattern failed to match your string input.

Also, you are using troublesome old date-time classes supplanted years ago by the java.time classes.

Your time-of-day hour lacks a padding zero. So you should have been using one h rather than two hh. Furthermore, a lowercase h means 12-hour clock, where your input seems to be 24-hour clock given the lack of an AM/PM indicator. So an uppercase H is in order.

String input = "01/06/2015 8:20" ;  // Notice the lack of a padding zero on the hour.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu H:m" ) ;
LocalDateTime ldt = LocalDateTime.parse( input , f ) ;

ldt.toString(): 2015-06-01T08:20


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.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for 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

The pattern in your date String as well as in SimpleDateFormat() should match :-

SimpleDateFormat("dd/MM/yyyy hh:mm") will work with 01/06/2015 2:24

AND

SimpleDateFormat("yyyy/MM/dd HH:mm:ss") will work with 2015/06/01 08:20:15

For complete list, see Offical Oracle Doc here

AnkeyNigam
  • 2,810
  • 4
  • 15
  • 23
0
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Test {
    public static void main(String[] args) {
        String dateText = "18/07/2015 05:30";        
        DateFormat fechas = new SimpleDateFormat("dd/MM/yyyy hh:mm");
        try {
            Date parse = fechas.parse(dateText);
            System.out.println("Date : " + fechas.format(parse));
        } catch (ParseException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
    }    
}

I do this like that source code