我有一个应用程序,需要以秒为单位比较时间。
我想知道如何以秒为单位获取当前的UTC时间。
有人可以发布一个例子吗?我们怎样才能在Java中做到这一点?
答案 0 :(得分:16)
您可以使用它来获取您想要时间的时区传递时区
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
然后你可以在日历对象上调用你想要的任何东西
System.out.println(cal.get(Calendar.YEAR));
System.out.println(cal.get(Calendar.HOUR));
System.out.println(cal.get(Calendar.HOUR_OF_DAY));
System.out.println(cal.get(Calendar.MINUTE));
以下示例以秒为单位比较两个日历
Calendar cal1 = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
Calendar cal2 = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
// Set the dates for calendars
cal1.set(2011, 1, 1);
cal2.set(2011, 2, 2);
// Get the represented date in milliseconds as a long
long milis1 = cal1.getTimeInMillis();
long milis2 = cal2.getTimeInMillis();
// Calculate difference in milliseconds
long diff = milis2 - milis1;
// Calculate difference in seconds
long diffSecs = diff / 1000;
System.out.println("In seconds: " + diffSecs + " seconds");
答案 1 :(得分:11)
答案 2 :(得分:3)
Joda简化了一切
import org.joda.time.ReadableInstant;
import org.joda.time.DateTime;
import static org.joda.time.DateTimeZone.UTC;
import static org.joda.time.Seconds.secondsBetween;
...
ReadableInstant start = new DateTime("2011-01-01", UTC);
ReadableInstant end = new DateTime("2011-02-02", UTC);
int secondsDifference = secondsBetween(start, end).getSeconds();
答案 3 :(得分:2)
public static long getUtcTime(long time) {
System.out.println("Time="+time);
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date dbefore=new Date(time);
System.out.println("Date before conversion="+format.format(dbefore));
Calendar c = Calendar.getInstance();
c.setTimeInMillis(time);
TimeZone timezone = c.getTimeZone();
int offset = timezone.getRawOffset();
if(timezone.inDaylightTime(new Date())){
offset = offset + timezone.getDSTSavings();
}
int offsetHrs = offset / 1000 / 60 / 60;
int offsetMins = offset / 1000 / 60 % 60;
System.out.println("offset: " + offsetHrs);
System.out.println("offset: " + offsetMins);
c.add(Calendar.HOUR_OF_DAY, (-offsetHrs));
c.add(Calendar.MINUTE, (-offsetMins));
System.out.println("Date after conversion: "+format.format(c.getTime()));
System.out.println("Time converted="+c.getTime().getTime());
return c.getTime().getTime();
}
答案 4 :(得分:1)
以秒为单位获取当前UTC时间(自1.5起):
TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis())
根据Javadoc:
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/System.html#currentTimeMillis
返回:
当前时间与之间的差异,以毫秒为单位 UTC时间1970年1月1日午夜。
答案 5 :(得分:0)
要将日期存储为整数而不是长整数,可以除以1000,还可以选择除以另一个日期,例如2019年1月1日:
private int getUTC()
{
Calendar cal1 = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
cal1.set(2019, 1, 1);
long millis1 = cal1.getTimeInMillis();
//Current date in milliseconds
long millis2 = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
// Calculate difference in seconds
long diff = millis2 - millis1;
return (int)(diff / 1000);
}
答案 6 :(得分:-1)
这有效:
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
GregorianCalendar gregorianCalendar = new GregorianCalendar
(
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH),
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE),
calendar.get(Calendar.SECOND)
);
return gregorianCalendar.getTime();