Cosa mi manca? Date() è il numero di millisecondi che sono trascorsi da mezzanotte, 1 gennaio 1970. La mezzanotte non dovrebbe iniziare alle 0:00?Java time: inizia il 1 gennaio 1970 alle 1:00?
Riferimenti:
- http://www.tutorialspoint.com/java/java_date_time.htm
- https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html
- https://docs.oracle.com/javase/7/docs/api/java/sql/Time.html
Il mio programma di test:
package be.test.package.time;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
public class TimeWork {
public static void main(String[] args) {
List<Long> longs = new ArrayList<>();
List<String> strings = new ArrayList<>();
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss.SSS");
//Now
Date now = new Date();
strings.add(formatter.format(now));
//Test dates
strings.add("01-01-1970 00:00:00.000");
strings.add("01-01-1970 01:00:00.000");
strings.add("31-11-1969 00:00:00.000");
strings.add("01-01-2014 00:00:00.000");
//Test data
longs.add(-1L);
longs.add(0L);
longs.add(1L);
longs.add(7260000L);
longs.add(1417706084037L);
longs.add(-7260000L);
//Show the long value of the date
for (String string: strings) {
try {
Date date = formatter.parse(string);
System.out.println("Formated date : " + string + " = Long = " + date.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
}
//Show the date behind the long
for (Long lo : longs) {
Date date = new Date(lo);
String string = formatter.format(date);
System.out.println("Formated date : " + string + " = Long = " + lo);
}
}
}
Questo sono i risultati:
Formated date : 05-12-2014 08:54:59.318 = Long = 1417766099318
Formated date : 01-01-1970 00:00:00.000 = Long = -3600000
Formated date : 01-01-1970 01:00:00.000 = Long = 0
Formated date : 31-11-1969 00:00:00.000 = Long = -2682000000
Formated date : 01-01-2014 00:00:00.000 = Long = 1388530800000
Formated date : 01-01-1970 12:59:59.999 = Long = -1
Formated date : 01-01-1970 01:00:00.000 = Long = 0
Formated date : 01-01-1970 01:00:00.001 = Long = 1
Formated date : 01-01-1970 03:01:00.000 = Long = 7260000
Formated date : 04-12-2014 04:14:44.037 = Long = 1417706084037
Formated date : 31-12-1969 10:59:00.000 = Long = -7260000
Perché è:
Formated date : 01-01-1970 01:00:00.000 = Long = 0
questo è al 01:00. Mi aspettavo 0:00.
Qual è il fuso orario? –
Il mio fuso orario è CET - Ora di Bruxelles/Belgio. –
Sei in anticipo sui tuoi tempi :-) –