0

I am trying to convert a date string of format dd.MM.YYYY into date object as following:

String start_dt = "01.01.2011";
DateFormat formatter = new SimpleDateFormat("dd.MM.YYYY");
Date date = (Date)formatter.parse(start_dt);
System.out.print("Date is : "+date); 

But I am getting this result Date is : Sun Dec 26 00:00:00 MST 2010 I tried it in different ides and even on compileonline.com , still same result. So Am I doing anything wrong here, because its not suppose to behave like this. Please help.

4 Answers 4

5

The pattern for year is incorrect. You need to say:

DateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
Sign up to request clarification or add additional context in comments.

Comments

2

The correct representation is yyyy

            String start_dt = "01.01.2011";
    DateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
    Date date = null;
    try {
        date = (Date)formatter.parse(start_dt);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.print("Date is : "+date); 

Comments

2
 public static void main(String[] args) {
        try {
            String start_dt = "01.01.2011";
         DateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
         Date date = (Date)formatter.parse(start_dt);
         System.out.print("Date is : "+date);
        } catch (ParseException ex) {
            Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

Comments

2

you should learn about DateFormat from here link1 and link2, you will realize that your code should be like that (year should be written in small letters).

String start_dt = "01.01.2011";
DateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
Date date = (Date)formatter.parse(start_dt);
System.out.print("Date is : "+date); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.