Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.text.ParseException: Unparseable date: "20050327020000"

Tags:

java

date

parsing

I have a problem with a function to validate dates.

When I put a date to the time 2am, puts the

java.text.ParseException error: unparseable date: " 20050327020000 ",

however should put a different time if it works correctly and I returned: Sun Mar 27 1:00:00 CET 2005.

I put the code below.

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

public class Principal {
/**
 * @param args
 */
public static void main(String[] args) {
    try {
        String format="yyyyMMddHHmmss";
        String date = "20050327010000";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        sdf.setTimeZone(TimeZone.getDefault());
        sdf.setLenient(false);
        Date d = sdf.parse(date);
        System.out.println("date parsed: " +d.toString());

    } catch (Exception e) {
        System.out.println("Exception: " +e.toString());
    }
  }
}
like image 703
miguel lopez Avatar asked Dec 13 '25 16:12

miguel lopez


1 Answers

It's a daylight savings problem.

2005-03-27 02:00:00 is a date and time that does not exist in your timezone (CET) - in that night, daylight savings shifted, and the clock jumped from 01:59:59 to 03:00:00.

like image 79
Jesper Avatar answered Dec 15 '25 06:12

Jesper



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!