Quick response:
No, you are not allowed to do this. . Date used for this.
From javadoc Date :
The Date class represents a specific point in time accurate to the millisecond.
However , since this class is just a data object. The dose does not care about how we describe it. When we see the date 2012/01/01 12:05:10.321 , we can say that this is 2012/01/01 , this is what you need. There are many ways to do this.
Example 1: string manipulation
Input line: 2012/01/20 12: 05: 10.321
Desired output line: 2012/01/20
Since yyyy / MM / dd is exactly what we need, we can just manipulate the string to get the result.
String input = "2012/01/20 12:05:10.321"; String output = input.substring(0, 10);
Example 2: by SimpleDateFormat
Input line: 2012/01/20 12: 05: 10.321
Desired output line: 01/20/2012
In this case, we need a different format.
String input = "2012/01/20 12:05:10.321"; DateFormat inputFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS"); Date date = inputFormatter.parse(input); DateFormat outputFormatter = new SimpleDateFormat("MM/dd/yyyy"); String output = outputFormatter.format(date);
To use SimpleDateFormat , check out the SimpleDateFormat JavaDoc .
Rangi Lin Mar 22 '12 at 5:20 2012-03-22 05:20
source share