How to get the current UTC time in seconds

2020-08-10 08:08发布

I have an application, which needs to compare the time in seconds.

I want to know how to get the current UTC time in seconds.

Can some one post an example of it how can we do this in Java?

7条回答
forever°为你锁心
2楼-- · 2020-08-10 08:14

Get current UTC time in seconds (since 1.5) :

TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis())

according to Javadoc:

http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/System.html#currentTimeMillis

Returns:

the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.

查看更多
啃猪蹄的小仙女
3楼-- · 2020-08-10 08:17

This works:

    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();
查看更多
Evening l夕情丶
4楼-- · 2020-08-10 08:18
 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();


    }
查看更多
狗以群分
5楼-- · 2020-08-10 08:28

You can use this to get timezone passing in timezone you want time back in

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC")); 

Then you can call whatever you want on the calendar object

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));

Below example to compare two calendars in seconds

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");
查看更多
三岁会撩人
7楼-- · 2020-08-10 08:31

To store the date as an integer instead of long, you can divide by 1000 and optionally subtract by another date, such as Jan. 1 2019:

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);
}
查看更多
登录 后发表回答