![]() |
灰常酷的蘑菇 · 【加解密】私钥文件解析_不知为MUJI的博客 ...· 1 年前 · |
![]() |
强悍的小刀 · laravel event ...· 1 年前 · |
![]() |
激动的警车 · BeaufulSoup获取特定标签下内容的方 ...· 1 年前 · |
我解析了一个来自
String
的
java.util.Date
,但它将本地时区设置为
date
对象的时区。
在从中分析
Date
的
String
中未指定时区。我想设置
date
对象的特定时区。
我该怎么做呢?
使用DateFormat。例如,
SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
isoFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = isoFormat.parse("2010-05-23T09:01:02");
请注意,
java.util.Date
对象本身不包含任何时区信息-您不能在
Date
对象上设置时区。
Date
对象唯一包含的是自“纪元”以来的毫秒数--1970年1月1日,世界时00:00:00。
如ZZ Coder所示,您在
DateFormat
对象上设置时区,以告诉它您希望在哪个时区中显示日期和时间。
您还可以在JVM级别设置时区
Date date1 = new Date();
System.out.println(date1);
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
// or pass in a command line arg: -Duser.timezone="UTC"
Date date2 = new Date();
System.out.println(date2);
输出:
Thu Sep 05 10:11:12 EDT 2013
Thu Sep 05 14:11:12 UTC 2013
如果您必须仅使用标准JDK类,则可以使用以下代码:
/**
* Converts the given <code>date</code> from the <code>fromTimeZone</code> to the
* <code>toTimeZone</code>. Since java.util.Date has does not really store time zome
* information, this actually converts the date to the date that it would be in the
* other time zone.
* @param date
* @param fromTimeZone
* @param toTimeZone
* @return
public static Date convertTimeZone(Date date, TimeZone fromTimeZone, TimeZone toTimeZone)
long fromTimeZoneOffset = getTimeZoneUTCAndDSTOffset(date, fromTimeZone);
long toTimeZoneOffset = getTimeZoneUTCAndDSTOffset(date, toTimeZone);
return new Date(date.getTime() + (toTimeZoneOffset - fromTimeZoneOffset));
* Calculates the offset of the <code>timeZone</code> from UTC, factoring in any
* additional offset due to the time zone being in daylight savings time as of
* the given <code>date</code>.
* @param date
* @param timeZone
* @return
private static long getTimeZoneUTCAndDSTOffset(Date date, TimeZone timeZone)
long timeZoneDSTOffset = 0;
if(timeZone.inDaylightTime(date))
timeZoneDSTOffset = timeZone.getDSTSavings();
return timeZone.getRawOffset() + timeZoneDSTOffset;
}
功劳归功于这个 post 。
如果有人需要这样做,如果您需要将
XMLGregorianCalendar
时区从协调世界时转换为您当前的时区,那么您需要做的就是将时区设置为
0
,然后调用
toGregorianCalendar()
-它将保持相同的时区,但
Date
知道如何将其转换为您的时区,因此您可以从那里获取数据。
XMLGregorianCalendar xmlStartTime = DatatypeFactory.newInstance()
.newXMLGregorianCalendar(
((GregorianCalendar)GregorianCalendar.getInstance());
xmlStartTime.setTimezone(0);
GregorianCalendar startCalendar = xmlStartTime.toGregorianCalendar();
Date startDate = startCalendar.getTime();
XMLGregorianCalendar xmlStartTime = DatatypeFactory.newInstance()
.newXMLGregorianCalendar(startCalendar);
xmlStartTime.setHour(startDate.getHours());
xmlStartTime.setDay(startDate.getDate());
xmlStartTime.setMinute(startDate.getMinutes());
xmlStartTime.setMonth(startDate.getMonth()+1);
xmlStartTime.setTimezone(-startDate.getTimezoneOffset());
xmlStartTime.setSecond(startDate.getSeconds());
xmlStartTime.setYear(startDate.getYear() + 1900);
System.out.println(xmlStartTime.toString());
结果:
2015-08-26T12:02:27.183Z
2015-08-26T14:02:27.183+02:00
将日期转换为字符串,并使用SimpleDateFormat执行此操作。
SimpleDateFormat readFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
readFormat.setTimeZone(TimeZone.getTimeZone("GMT" + timezoneOffset));
String dateStr = readFormat.format(date);
SimpleDateFormat writeFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date date = writeFormat.parse(dateStr);
这段代码对我正在开发的一个应用程序很有帮助:
Instant date = null;
Date sdf = null;
String formatTemplate = "EEE MMM dd yyyy HH:mm:ss";
try {
SimpleDateFormat isoFormat = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss");
isoFormat.setTimeZone(TimeZone.getTimeZone(ZoneId.of("US/Pacific")));
sdf = isoFormat.parse(timeAtWhichToMakeAvailable);
date = sdf.toInstant();
} catch (Exception e) {
System.out.println("did not parse: " + timeAtWhichToMakeAvailable);
LOGGER.info("timeAtWhichToMakeAvailable: " + timeAtWhichToMakeAvailable);
LOGGER.info("sdf: " + sdf);
LOGGER.info("parsed to: " + date);
在这里,您可以获取类似于"2020-03-11T20:16:17“的日期,并返回"11/Mar/2020 - 20:16”
private String transformLocalDateTimeBrazillianUTC(String dateJson) throws ParseException {
String localDateTimeFormat = "yyyy-MM-dd'T'HH:mm:ss";
SimpleDateFormat formatInput = new SimpleDateFormat(localDateTimeFormat);
//Here is will set the time zone
formatInput.setTimeZone(TimeZone.getTimeZone("UTC-03"));