Asked  7 Months ago    Answers:  5   Viewed   26 times

The following code is giving me the parsed date as "Wed Jan 13 00:00:00 EST 2010" instead of "Wed Jun 13 00:00:00 EST 2010". Any ideas much appreciated.

    SimpleDateFormat sf = new SimpleDateFormat("yyyy-mm-dd'T'HH:mm:ss");

    String str = "2010-06-13T00:00:00";

    Date date = sf.parse(str);

    System.out.println(" Date " + date.toString());

 Answers

30

Try:

"yyyy-MM-dd'T'HH:mm:ss"

MM means month. mm means minutes. See the documentation for SimpleDateFormat for more details of the supported date and time patterns.

Tuesday, June 1, 2021
 
VostanAzatyan
answered 7 Months ago
86

YYYY should be yyyy-

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.ENGLISH);

Please check the documentation for SimpleDateFormat here
Java 6 : http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
Java 7 : http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Friday, June 4, 2021
 
Whakkee
answered 7 Months ago
49

Change the pattern string from "yyyy/MM/DD" to "yyyy/MM/dd"

SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
Sunday, June 6, 2021
 
mcography
answered 6 Months ago
36

SimpleDateFormat API:

For parsing, if the number of pattern letters is more than 2, the year is interpreted literally, regardless of the number of digits. So using the pattern "MM/dd/yyyy", "01/11/12" parses to Jan 11, 12 A.D.

As for lenient, when it's set to false parse throws exception for invalid dates, eg 01/32/12, while in lenient mode this date is treated as 02/01/12. SimpleDateFormat uses Calendar internally, details about leniency can be found in Calendar API.

Friday, August 6, 2021
 
kar
answered 4 Months ago
kar
27

Set the Leniency bit:

public void setLenient(boolean lenient)

Specify whether or not date/time parsing is to be lenient. With lenient parsing, the parser may use heuristics to interpret inputs that do not precisely match this object's format. With strict parsing, inputs must match this object's format.

The following code:

import java.text.ParseException;
import java.text.SimpleDateFormat;

public class Tester {
    public static void main(String[] argv) {
        java.util.Date date;
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

        // Lenient
        try {
            date = sdf.parse("40/02/2013");
            System.out.println("Lenient date is :                  "+date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        // Rigorous
        sdf.setLenient(false);

        try {
            date = sdf.parse("40/02/2013");
            System.out.println("Rigorous date (won't be printed!): "+date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }
}

Gives:

Lenient date is :                  Tue Mar 12 00:00:00 IST 2013
java.text.ParseException: Unparseable date: "40/02/2013"
    at java.text.DateFormat.parse(DateFormat.java:357)

Notes

  1. When in doubt about a Java class, reading the class documentation should be your first step. I didn't know the answer to your question, I just Googled the class, clicked on the parse method link and noted the See Also part. You should always search first, and mention your findings in the question
  2. Lenient dates have a respectable history of bypassing censorship and inspire children's' imagination.
Saturday, August 7, 2021
 
DMTintner
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share