0

I am trying to store the value of date as 18/12/2016 into a date variable..

For example

    Date date=new Date(); 
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    Date theDate = null;

    try {
        theDate = dateFormat.parse(date.toString());
        System.out.println("Date parsed = " + dateFormat.format(theDate));
    } catch (ParseException e) {
        e.printStackTrace();
    }

But the above code gives an error

java.text.ParseException: Unparseable date: "Mon Jun 18 00:00:00 IST 2012"

I have found examples where we can change the format and store it in the string datatype. But I need an example where we can store it in the date format itself.

Hope you guys understand my question.Please help..

Thanks.

EDIT-All the links given shows how to store the date into a string.I need to store the date in the format 18/12/2016 inside the date datatype itself.So it is not a duplicate Question.

Benedict
  • 458
  • 2
  • 13

3 Answers3

0

No need to parse it. just format it when you want show the date.

SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date theDate = new Date();

try {
     System.out.println("Date parsed = " + dateFormat.format(theDate));
} catch (ParseException e) {
     e.printStackTrace();
}
Randyka Yudhistira
  • 3,612
  • 1
  • 26
  • 41
0

even though I think this is duplicated by this question I will answer in short.

SimpleDateFormatter is a way to display dates and not to store them, all dates should be stored and handled in a number format referring to UTC in order to prevent mistakes.

A fun side note is this youtube video about dealing with time that ephesises the need to deal with time in a unified way and then only format it to the users in the UI level.

Community
  • 1
  • 1
thepoosh
  • 12,497
  • 15
  • 73
  • 132
0

ou are wrong in the way you display the data I guess, because for me:

String dateString = "18/12/2016";
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date convertedDate = new Date();
try {
    convertedDate = dateFormat.parse(dateString);
} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
System.out.println(convertedDate);