Java 无法将UTC转换为本地时间
我正在编写一个Java代码。我能够将本地时间转换为UTC。但当我想将其转换回本地时间时,我面临着一个问题。我正在执行的步骤-Java 无法将UTC转换为本地时间,java,datetime,Java,Datetime,我正在编写一个Java代码。我能够将本地时间转换为UTC。但当我想将其转换回本地时间时,我面临着一个问题。我正在执行的步骤- 获取当前的毫秒数。(完成) 将当前毫秒(毫秒历元)转换为当前本地时间。(完成) 将当前毫秒(毫秒)转换为UTC时间。(完成) 将UTC时间转换为UTC毫秒。(完成) 将UTC毫秒转换为UTC时间。(完成) 将UTC毫秒转换为本地时间(IST)(这里面临的问题是….在这里获得相同的UTC时间。为什么不转换为本地时间?)。在创建多个日期对象时,有一个答案,但在这里我不创建多个
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.TimeZone;
public class TimeApiClass {
public static void main(String args[]) throws ParseException {
ZonedDateTime currentZone = ZonedDateTime.now();
//Getting milliseconds of the present
Instant instant=currentZone.toInstant();
long millisSinceEpoch=instant.toEpochMilli();
System.out.println("Not Converted to UTC-(Milliseconds-in local)");
System.out.println(millisSinceEpoch); //Output1->1579788244731
//Converting present milliseconds(millisSinceEpoch) to present local time
SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS ");
String str3=sdf3.format(new Date(millisSinceEpoch));
System.out.println("Local Time-Not Converted to UTC-(Present local time)");
System.out.println(str3); //2020/01/23 19:34:04.731
//Converting present milliseconds(millisSinceEpoch) to UTC time
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS ");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
String str=sdf.format(new Date(millisSinceEpoch));
System.out.println("UTC time-Local time Converted to UTC(unrequired time format)-");
System.out.println(str);// str contains 2020/01/22 13:22:55 UTC //Output3 ->2020/01/23 14:04:04.731
System.out.println("-----------------------------------------------------------------------------------------");
//Converting UTC time to UTC milliseconds
String myDate = str; // myDate and str contains 2020/01/22 10:08:42
SimpleDateFormat sdff = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS ");
Date date = sdff.parse(myDate);
long millis = date.getTime();
System.out.println("Converted to milliseconds(Now its in required format in UTC milliseconds)");
System.out.println(millis); //Output4 ->1579749867000
//Convert UTC milliseconds to UTC Time
DateFormat simple = new SimpleDateFormat(" yyyy/MM/dd HH:mm:ss.SSS");
Date result=new Date(millis);
System.out.println("Converting UTC milliseconds back to date/time format-");
System.out.println(simple.format(result));
//Convert UTC milliseconds to local time(IST)
Date dateee=new Date(millis); //Facing the problem here.....Getting the same UTC time here.Why is it not converting to Local time?
DateFormat format=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
format.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
System.out.println("lllllllllllllllllllllllll");
System.out.println(format.format(dateee));
//Converting UTC time to IST time
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS ");
sdf2.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
String istTime=sdf2.format(new Date(millis));
System.out.println("Now converting UTC millis to IST format->");
System.out.println(istTime); //Output5 ->2020/01/23 08:54:27 IST
//Converting IST time to milliseconds,here I am facing the problem.My output has to give the local milliseconds as I am passing ISTtime here,but its giving me UTC milliseconds,Which I dont want.
Date date66=sdf2.parse(istTime);
long finalMillis=date66.getTime();
System.out.println("Now converting IST to IST milliseconds:");
System.out.println(finalMillis); //Output6 ->1579749867000
}
}
//1579613838087ZoneDateTime
转换为不同的时区
不建议您使用
java.util.SimpleDataFormat
进行日期和时间操作,您的代码显示部分使用java.time
,这意味着您可以完全切换到java.time
。对于格式化,请使用java.time.format.DateTimeFormatter
请参见此示例:
public static void main(String[] args) {
// get the current instant
Instant instant = Instant.now();
// and convert it to a datetime object representing date and time in UTC
ZonedDateTime ofInstant = ZonedDateTime.ofInstant(instant, ZoneId.of("UTC"));
// print it
System.out.println("Now from Instant in UTC:\t"
+ ofInstant.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
// you can have a shorter way: current moment in your system's time zone
ZonedDateTime nowHere = ZonedDateTime.now();
// or in UTC
ZonedDateTime utcNow = ZonedDateTime.now(ZoneId.of("UTC"));
// print them both
System.out.println("Now in my (system's) time zone:\t"
+ nowHere.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
System.out.println("Now in UTC:\t\t\t"
+ utcNow.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
// then format the UTC datetime back to the one your system has
ZonedDateTime backFromUtc = utcNow.withZoneSameInstant(ZoneId.systemDefault());
// and print that one, too
System.out.println("Back from UTC:\t\t\t"
+ backFromUtc.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
}
输出类似于:
现在从UTC的即时开始:2020-01-23T15:44:11.921Z[UTC]
现在在我的(系统)时区:2020-01-23T16:44:12.091+01:00[欧洲/柏林]
现在UTC:2020-01-23T15:44:12.091Z[UTC]
从UTC返回:2020-01-23T16:50:52.194+01:00[欧洲/柏林]
为什么不转换成当地时间
出现错误的地方:字符串myDate
使用UTC,但是(1)字符串中没有UTC的指示,并且(2)您正在使用格式化程序sdff
解析它,但是此格式化程序没有使用UTC(它使用您的默认时区,印度标准时间)。因此,从解析中得到的结果是错误的
可能还有另一个误解:无论时区如何,纪元都是一个特定的时间点。因此,自历元起的毫秒在所有时区中都是相同的。因此,出现问题的第一个迹象可能是程序正在打印两个不同的毫秒值。如果毫秒值应该表示相同的时间点,那么两次应该看到相同的值
综上所述,deHaar在另一个答案中是正确的,即您应该远离所有DateFormat
、SimpleDateFormat
、Date
和TimeZone
。所有这些类的设计都很糟糕,而且早已过时。我认为您对上述类的使用使得代码变得比需要的复杂得多
编辑:
所以你告诉我不要使用DateFormat、SimpleDateFormat和
时区。那么你能建议我上哪一节课吗
理想使用
使用java.time中的Instant
和ZonedDateTime
时,您已经走上了正轨,这是现代java日期和时间API。这个API提供了您所需要的所有功能,而且使用起来非常方便,因此我建议您坚持使用它。您可能会发现其他有用的java.time类包括ZoneId
(而不是TimeZone
)和DateTimeFormatter
(而不是SimpleDateFormat
和DateFormat
)。有关更多信息,请参见教程和/或文档。稍后我将在底部添加链接
要将UTC字符串解析为自历元起的毫秒数,请执行以下操作:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss.SSS");
String utcString = "2020/01/22 13:22:55.731";
long millisSinceEpoch = LocalDateTime.parse(utcString, formatter)
.atOffset(ZoneOffset.UTC)
.toInstant()
.toEpochMilli();
System.out.println(millisSinceEpoch);
1579699375731
或者,如果您的真正目标是将其转换为印度当地时间:
ZoneId indiaStandardTime = ZoneId.of("Asia/Kolkata");
ZonedDateTime timeInIndia = LocalDateTime.parse(utcString, formatter)
.atOffset(ZoneOffset.UTC)
.atZoneSameInstant(indiaStandardTime);
System.out.println(timeInIndia);
2020-01-22T18:52:55.731+05:30[亚洲/加尔各答]
我首先告诉Java以UTC解释字符串,然后将其转换为IST
链接
- 解释如何使用java.time
LocalDate
或LocalDateTime
。它们应该能很好地与ZoneDateTime
配合使用。好吧……你告诉我不要使用DateFormat、SimpleDateFormat和TimeZone。那么你能建议我理想使用哪一类吗?我也不能使用Calender,因为我读到它已经成为传统了。(提前感谢!)