-4
Date x=someobject.getLastRanDate();

System.out.println(x);

For the above code, I am getting the output in yyyy-MM-dd HH:mm:ss format. How is this happening or how can we do this ?

Note : getLastRanDate() method is getting the date from Database which is storing in the Date Variable.

PS : Please don't answer about SimpleDateFormat.parse(). I explicitly mentioned output variable should be Date object.

M.Nehru
  • 31
  • 2
  • 7
  • What is the problem exactly? – litelite May 04 '17 at 12:35
  • 2
    Use some `SimpleDateFormat` to format the date the way you want. The observed format comes from `Date#toString` –  May 04 '17 at 12:37
  • What is `Sysout`? –  May 04 '17 at 12:37
  • I need to know how we can store the data in yyyy-MM-dd HH:mm:ss format in a Date variable or how we can pass the date in yyyy-MM-dd HH:mm:ss as a Date data type. – M.Nehru May 04 '17 at 12:37
  • 1
    x is just a date, and what your get printed is one of several possible formats, you can change it as convenience, follow the @RC advice! – ΦXocę 웃 Пepeúpa ツ May 04 '17 at 12:41
  • `Date` is a simple epoch timestamp holder. When you call `toString()` it is parse to be readable. You can define you format like said before. – AxelH May 04 '17 at 12:42
  • 1
    I am aware that this isn't an **exact** duplicate. But the point is: as the already existing answers and comments point out, a **Date** object does not carry any formatting. And getting from strings, or to formatted strings for dates is explained in much detail in the dup'ed question. – GhostCat May 04 '17 at 12:47
  • Well. In My project, above one is achieved. I mean, if I print a Date data type is printing the output like "2017-03-31 12:00:00" instead of Fri Mar 31 00:00:00 CEST 2017. If there is no formatting for Date object, how it is achieved in the code I have ? – M.Nehru May 04 '17 at 12:56
  • Maybe you have some custom Date class, it's really easy using an IDE where the `toString` comes from. –  May 04 '17 at 13:34

2 Answers2

1

In Java, a Date represents a point in time. It does not have any format. When you execute System.out.println, it calls toString() method of Date class which formats the dates into String using default format.

You can use SimpleDateFormat class to format the Date differntly. However, as far as storing the date in database is concerned, you don't need to worry about the format (it's just a representation really), you can define the column type as Date or Datetime (depending on requirement and database type) and use any framework to persist the Date object straight away.

Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
  • Answer for my question is : Timestamp ts=new Timestamp(u1.getJoinedDate().getTime()); Date da=ts; System.out.println(da); In this scenario,date variable will print the timestamp formatted output(yyyy-MM-dd HH:mm:ss). – M.Nehru May 04 '17 at 16:30
  • @M.Nehru You need to let go of the notion that a date-time object such as `java.sql.Date`, `java.util.Date`, or `java.sql.Timestamp` have a format. They do *not* have a format because they are not strings! They each have a `toString` that *generates* a string to represent their value. To generate a String in a particular format, use a formatting class. You would have learned this if you had read the many hundreds of Q&A already posted to Stack Overflow. Please search Stack Overflow before posting. – Basil Bourque May 04 '17 at 17:42
  • @M.Nehru You are using troublesome old date-time classes that are now legacy, supplanted by the java.time classes. – Basil Bourque May 04 '17 at 17:44
0

Let's the documention explains itself with java.util.Date#toString()

Converts this Date object to a String of the form: 
     dow mon dd hh:mm:ss zzz yyyy

Returns:a string representation of this date.

So this String is just a representation of the date.

From the same page, you are invited to see java.text.DateFormat that will explain how to change the format but also give you a subclass java.text.SimpleDateFormat explaining every pattern accessible to define your own format.

I can summarize the links more but the javadoc is quite resistant to time (I hope so)

AxelH
  • 14,325
  • 2
  • 25
  • 55