Michael Schmidt
Michael Schmidt

Reputation: 9230

Format Date not to a String

I want to format the current date to this pattern dd.MM.yyy. At this time, I use:

SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
Date now = new Date();
String start = dateFormat.format(now);

And I must do this for a lot of dates. The problem is that I need the formatted date as Date, not as a String.

I don't find out, how this is possible in a easy way (there are about 150 - 200 dates to format).

Upvotes: 0

Views: 96

Answers (3)

user2087103
user2087103

Reputation: 161

But, what do you want? what do you expect?
A date is just miliseconds, you can't have a date formatted. You can parse it from a string (with the SimpleDateFormat), you can show it in a parsed way (in string), but underneath, you only have a miliseconds.
So, you can have 20.06.2013 by

    SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
    Date now = new Date();
    System.out.println(dateFormat.format(now));

If you just want a date without hours, minutes and seconds, then use ZouZou approach. Or JodaTime.

Upvotes: 0

wobblycogs
wobblycogs

Reputation: 4093

SimpleDateFormat is used to convert a Date into a String

String myFormattedDate = dateFormat.format( someDate );

representation or a String into a Date

Date myDate = dateFormat.parse( someString)

Internally a Date is just a long millisecond value so you can't view it as a formatted string unless you use a formatter of some kind.

As for there being a lot of dates, 200 will take millisecond to process so don't worry about that.

Upvotes: 4

Alexis C.
Alexis C.

Reputation: 93892

Then use dateFormat.parse(start);

SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
Date now = new Date();
System.out.println(now.toString());
String start = dateFormat.format(now);
Date after = dateFormat.parse(start);
System.out.println(after.toString());

Output :

Thu Jun 20 11:01:12 CEST 2013 
Thu Jun 20 00:00:00 CEST 2013

Upvotes: 2

Related Questions