String ip="2011-05-01T06:47:35.422-05:00";
ZonedDateTime mzt = ZonedDateTime.parse(ip).toInstant().atZone(ZoneOffset.UTC);
System.out.println(mzt);
System.out.println("-----");
String ip2="2011-05-01T00:00:00.000-05:00";
ZonedDateTime mzt2 = ZonedDateTime.parse(ip2).toInstant().atZone(ZoneOffset.UTC);
System.out.println(mzt2);
Run Code Online (Sandbox Code Playgroud)
输出:
2011-05-01T11:47:35.422Z
-----
2011-05-01T05:00Z
Run Code Online (Sandbox Code Playgroud)
为什么在案例2中更改日期格式?由于这个原因,我收到SQLServer数据库错误.
我试图使用 ZonedDateTime.ofInstant 给出特定的日期时间来获取时间,但是我注意到在某些情况下给出的结果不包含秒。代表我正在处理的情况的代码示例
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a long variable for milliseconds
//this is the same as "2022-01-31T00:00:00.00Z"
long milliseconds1 = 1643587200000L;
long milliseconds2 = 1643587201000L;
// get Instant using ofEpochMilli() method
Instant instant1
= Instant.ofEpochMilli(milliseconds1);
// create Instant object
Instant instant2
= Instant.ofEpochMilli(milliseconds2);
// create a ZonID
ZoneId zone = ZoneId.of("Europe/Lisbon");
// apply ofInstant method
// of ZonedDateTime class
ZonedDateTime zt1 = ZonedDateTime
.ofInstant(instant1, zone);
ZonedDateTime zt2 = ZonedDateTime …Run Code Online (Sandbox Code Playgroud)