Confused about Date
Hello,
I'm a bit confused about Date. In the example below, I get the present
Date, format it to Strings using DateFormaters, reparse the String using
the _same_ DateFormater and recombine them by adding.
The ouput is not the same as the input. Why?
In my locale it outputs:
Before: 23.10.07 19:16:49
After: 23.10.07 18:16:49
Thanks Phil
-- SSCCE --
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
public class TimeTest {
public static void main(String[] args) {
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
DateFormat tf = DateFormat.getTimeInstance(DateFormat.MEDIUM);
DateFormat dtf =
DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.MEDIUM);
// getting actual time
Date now = new Date();
System.out.println("Before: "+ dtf.format(now));
// separating in date and time strings
String dateString = df.format(now);
String timeString = tf.format(now);
Date date = null;
Date time = null;
try {
// parsing date and time strings
date = df.parse(dateString);
time = tf.parse(timeString);
} catch (ParseException e) {
e.printStackTrace();
}
// recombining date and time
Date after = new Date(date.getTime() + time.getTime());
System.out.println("After: "+ dtf.format(after));
}
}