How can I write DateTime.UtcNow.Ticks from C# in java

帅比萌擦擦* 提交于 2021-01-20 13:41:10

问题


I am trying to rewrite below two lines of code from C# into Java.

long ticks1970Onwards = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).Ticks;
 long newTs =    (DateTime.UtcNow.Ticks - ticks1970Onwards)/10000;

I tried multiple ways , but I don't get the correct solution.

        ZonedDateTime dt1 = LocalDateTime.now().atZone(ZoneId.of("UTC"));
        ZonedDateTime dt2 = LocalDateTime.of(1901, 1, 1, 0, 0).atZone(ZoneId.of("UTC"));
        Duration duration2 = Duration.between(dt2, dt1);
        System.out.printf("Duration = %s milliseconds.%n", duration2.getSeconds()*1000);

回答1:


Instant#toEpochMilli

Use it to convert Instant.now() to the number of milliseconds from the epoch of 1970-01-01T00:00:00Z where Z stands for Zulu time and represents UTC.

import java.time.Instant;

public class Main {
    public static void main(String[] args) {
        Instant now = Instant.now();
        System.out.println(now.toEpochMilli());
    }
}

You can compare this result with that of the C# code at C# PlayGround.

Note: Avoid specifying a timezone with the 3-letter abbreviation. A timezone should be specified with a name in the format, Region/City e.g. ZoneId.of("Europe/London"). With this convention, the ZoneId for UTC can be specified with ZoneId.of("Etc/UTC"). A timezone specified in terms of UTC[+/-]Offset can be specified as Etc/GMT[+/-]Offset e.g. ZoneId.of("Etc/GMT+1"), ZoneId.of("Etc/GMT+1") etc.

There are some exceptional cases as well e.g. to specify the timezone of Turkey, you can specify

ZoneId.of("Turkey")

However, it is recommended (Thanks to Ole V.V.) to use the same pattern as described above i.e. the recommended way of representing the timezone of Turkey is:

ZoneId.of("Asia/Istanbul")

or

ZoneId.of("Europe/Istanbul")

Probably, Istanbul being referred with two regions/continents is the reason why it is still returned by ZoneId.getAvailableZoneIds().

The following code will give you all the available ZoneIds:

// Get the set of all time zone IDs.
Set<String> allZones = ZoneId.getAvailableZoneIds();



回答2:


Calendar calendar = Calendar.getInstance(); 
calendar.set(Calendar.MILLISECOND, 0); // Clear the millis part. Silly API. 
calendar.set(2010, 8, 14, 0, 0, 0); // Note that months are 0-based 
Date date = calendar.getTime(); 
long millis = date.getTime(); // Millis since Unix epoch 
  1. example

import java.util.*;

public class Test {

private static final long TICKS_AT_EPOCH = 621355968000000000L; 
private static final long TICKS_PER_MILLISECOND = 10000; 

public static void main(String[] args) { 
 long ticks = 634200192000000000L; 

 Date date = new Date((ticks - TICKS_AT_EPOCH)/TICKS_PER_MILLISECOND); 
 System.out.println(date); 

 TimeZone utc = TimeZone.getTimeZone("UTC"); 
 Calendar calendar = Calendar.getInstance(utc); 
 calendar.setTime(date); 
 System.out.println(calendar); 
} 

}

  1. example

    import java.util.Calendar; import java.util.Date;

    public class DateHelper {

     private static final long TICKS_AT_EPOCH = 621355968000000000L; 
     private static final long TICKS_PER_MILLISECOND = 10000; 
    
     public static long getUTCTicks(Date date){ 
    
      Calendar calendar = Calendar.getInstance(); 
      calendar.setTime(date); 
    
      return (calendar.getTimeInMillis() * TICKS_PER_MILLISECOND) + TICKS_AT_EPOCH; 
    
     } 
    
     public static Date getDate(long UTCTicks){ 
    
      return new Date((UTCTicks - TICKS_AT_EPOCH)/TICKS_PER_MILLISECOND); 
    
     } 
    


来源:https://stackoverflow.com/questions/65534226/how-can-i-write-datetime-utcnow-ticks-from-c-sharp-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!