Reputation: 141
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?
Upvotes: 2
Views: 18939
Reputation: 339362
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
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
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.
Upvotes: 1
Reputation: 1
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
Upvotes: 0
Reputation: 2820
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
Upvotes: 0
Reputation: 32323
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)
Upvotes: 2