-1

I have a Date object as follows:

java.util.Date d = new java.util.Date(System.currentTimeMillis()); // Mon Dec 23 14:57:28 PST 2013

I need to format the date to get another Date object with this format instead: 2013-12-23 14:57:28

I tried this code:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
sdf.format(d); // d is still Mon Dec 23 14:57:28 PST 2013, no formatting.

I tried this code:

String s = d.toString();
try {           
        d = sdf.parse(s);
    } catch (Exception e) 
        e.printStackTrace(); // java.text.ParseException: Unparseable date: "Mon Dec 23 14:35:48 PST 2013"

Would you please tell me what am I doing wrong? I googled searched it but the solutions to format a Date was more or less what I tried. Any help is greatly appreciated.

Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
blueSky
  • 649
  • 5
  • 13
  • 31

2 Answers2

3

You don't understand what a Date is, and what format() does. A Date is just a number of milliseconds. Nothing more. It doesn't have any format. Formatting a date doesn't change the date at all. It returns a string containing a human readable representation of the date (like "2012-11-23" or "Monday, April 2").

So, the following instruction:

sdf.format(d);

is effectively a noop. You ignore the string that it returns.

If what you want is to have a specific format used when calling date.toString(), it's impossible. When you want to display a date in a specific format (yyyy-MM-dd for example), instead of doing

System.out.println(date);

use

DateFormat format = new SimpleDaeFormat("yyyy-MM-dd");
System.out.println(format.format(date));

All this is clearly explained in the javadoc. You should read it.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • @blueSky: ignore my answer, I didn't read the question fully. This is the one you want. – Hovercraft Full Of Eels Dec 23 '13 at 23:25
  • @JB Nizet: You are right. I do get the formatted Date in the String returned from format() method. However, I still do not know why: d = sdf.parse(s); should throw an exp.? What if I need a Date object - not a String - with this format: 2013-12-23 15:24:21 ? – blueSky Dec 23 '13 at 23:29
  • Ok, got it. Can somebody please explain why do I get "java.text.ParseException: Unparseable date"? What am I doing wrong? How that method should be used that does NOT throw an exp.? – blueSky Dec 23 '13 at 23:36
  • Because Date.toString(), that you're trying to parse with a format initialized with the pattern "yyyy-mm-dd HH:mm:ss", doesn't return a String that matches this pattern. – JB Nizet Dec 24 '13 at 12:58
0

SimpleDateFormat, does not change the date format, it gives you a formatted date for display purpose only, not for anything else.

util.Date will always have one format (a long number of milliseconds) that you can format to any way you want in order to display using SimpleDateFormat. So in effect no matter what date you get you can format to what format you want.

If you explain why you are trying to do what you are trying to do, then maybe we can support you better.

IndikaM
  • 409
  • 7
  • 14
  • I have a Date which is actually like: "Mon Dec 23 15:24:21 PST 2013", need to have it formatted like: 2013-12-23 15:24:21 to save it in the database (sql server) in a field of type: datetime. I guess I can do this with the formatted String I've got - not sure if I necessary need: java.sql.Date - this was the reason I was trying to use the SimpleDateFormat.parse() method to get Date object instead. Regardless, I still don't know how the parse() method should be used that it does not throw an exp. I think I'm doing something wrong and this is why it throws the exp. – blueSky Dec 23 '13 at 23:47
  • When saving to DB, are you going to use direct SQL's or an ORM like hibernate? – IndikaM Dec 24 '13 at 00:12
  • Ok, got it. Thanks for all time and responses. – blueSky Dec 24 '13 at 00:13
  • @IndiikaM: I insert all values in csv file and then bulk insert into DB. I'll try - when my I have an String for a Date - and if that does not work, try converting the String object to java.sql.Date. – blueSky Dec 24 '13 at 00:15
  • If you are using an SQL statment, you can just format the util.Date that you have into the format you want and run the SQL. Date mDate = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss"); String SQL = "INSERT INTO MYTABLE (.... ,DATE,....)VALUES (....'"+sdf.format(mDate)+"'.....)"; – IndikaM Dec 24 '13 at 10:52