我正在尝试运行Java程序,但它采用默认的GMT时区而不是OS定义的时区.我的JDK版本是1.5,操作系统是Windows Server Enterprise(2007)
Windows指定了一个中央时区,但是当我运行以下程序时,它会给我一个GMT时间.
import java.util.Calendar;
public class DateTest
{
public static void main(String[] args)
{
Calendar now = Calendar.getInstance();
System.out.println(now.getTimeZone());
System.out.println(now.getTime());
}
}
Run Code Online (Sandbox Code Playgroud)
这是输出
sun.util.calendar.ZoneInfo[id="GMT",
offset=0,
dstSavings=0,
useDaylight=false,
transitions=0,
lastRule=null]
Mon Mar 22 13:46:45 GMT 2010
Run Code Online (Sandbox Code Playgroud)
请注意,我不想从应用程序设置时区.我希望JVM使用的时区应该是操作系统中指定的时区.(我没有发现其他具有1.4版JDK和Microsoft Server 2003的服务器的问题).
任何想法都将受到高度赞赏.
我使用此下拉列表为应用程序存储与UTC相关的时区:
<select id="timezone" name="timezone" >
<option value="-12">[UTC - 12] Baker Island Time</option>
<option value="-11">[UTC - 11] Niue Time, Samoa Standard Time</option>
<option value="-10">[UTC - 10] Hawaii-Aleutian Standard Time, Cook Island Time</option>
<option value="-9.5">[UTC - 9:30] Marquesas Islands Time</option>
<option value="-9">[UTC - 9] Alaska Standard Time, Gambier Island Time</option>
<option value="-8">[UTC - 8] Pacific Standard Time</option>
<option value="-7">[UTC - 7] Mountain Standard Time</option>
<option value="-6">[UTC - 6] Central Standard Time</option>
<option value="-5">[UTC - 5] Eastern Standard Time</option>
<option value="-4.5">[UTC …Run Code Online (Sandbox Code Playgroud) 我现在创建日期:
ZoneId gmt = ZoneId.of("GMT");
LocalDateTime localDateTime = LocalDateTime.now();
LocalDate localDateNow = localDateTime.toLocalDate();
Run Code Online (Sandbox Code Playgroud)
然后我希望以毫秒为单位返回此日期:
localDateNow.atStartOfDay(gmt) - 22.08.2017
localDateNow.atStartOfDay(gmt).toEpochSecond(); - 1503360000 (18.01.70)
Run Code Online (Sandbox Code Playgroud)
我怎么能LocalDate.now()在几毫秒内返回?
我正在尝试编写一个泛型方法来返回ZonedDateTime给定的日期String及其格式.
如果未在日期中指定,我们如何ZonedDateTime使用默认值?ZoneIdString
它可以用java.util.Calendar,但我想使用Java 8时间API.
这里的问题是使用固定的时区.我将格式指定为参数.日期及其格式都是String参数.更通用.
代码和输出如下:
public class DateUtil {
/** Convert a given String to ZonedDateTime. Use default Zone in string does not have zone. */
public ZonedDateTime parseToZonedDateTime(String date, String dateFormat) {
//use java.time from java 8
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat);
ZonedDateTime zonedDateTime = ZonedDateTime.parse(date, formatter);
return zonedDateTime;
}
public static void main(String args[]) {
DateUtil dateUtil = new DateUtil();
System.out.println(dateUtil.parseToZonedDateTime("2017-09-14 15:00:00+0530", "yyyy-MM-dd HH:mm:ssZ"));
System.out.println(dateUtil.parseToZonedDateTime("2017-09-14 …Run Code Online (Sandbox Code Playgroud) 如果我用java.util.Date的toInstant()上这恰好是一个变量java.sql.Date,我得到一个UnsupportedOperationException。
try {
java.util.Date input = new java.sql.Date(System.currentTimeMillis());
LocalDate date = input.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
} catch (UnsupportedOperationException e) {
// grrr!
}
Run Code Online (Sandbox Code Playgroud)
的java.util.Date,我很担心经由传统API的MySQL数据库来自一个日期字段,而实际上是一个java.sql.Date。
现在,以下相关问题都很有趣:
UnsupportedOperationException-为什么不能在java.sql.Date上调用toInstant()?
将java.util.Date转换为java.time.LocalDate
LocalDate到java.util.Date的转换,反之亦然?
但它们没有提供任何截断a的优雅方法java.util.Date来摆脱时间成分并获得Java 8 LocalDate。
我承认存在一个问题,一个时区中的同一时刻可能与另一时区中的同一时刻的日期不同。
我怀疑解决方案将涉及java.util.Calendar,但与其设计我自己的解决方案,不如先确定其他人已经做的事情。
我希望找到比这短的东西:
Run Code Online (Sandbox Code Playgroud)Date date = new Date(); // timestamp now Calendar cal = Calendar.getInstance(); // get calendar instance cal.setTime(date); // set cal to date cal.set(Calendar.HOUR_OF_DAY, 0); // set hour to midnight cal.set(Calendar.MINUTE, 0); …
我需要在 Java 8 中模拟时间(到特定日期,这意味着我可能不会使用anyLong())以java.time在 EasyMock 下的测试环境中单独使用,因此没有 Joda Time。
EasyMock 不允许你模拟像LocalDateTime.now(). PowerMock 可以,但我不允许使用 PowerMock。
Joda Time 库提供了一种简洁优雅的方式来模拟时间DateTimeUtils.setCurrentMillisFixed(),并DateTimeUtils.setCurrentMillisSystem()回到现在的瞬间。
只有我不允许使用 Joda Time。
我被告知使用模拟时钟java.time.Clock类实例并将其注入LocalDateTime.now()如下:LocalDateTime.now(mockClock)
有没有办法在没有 Joda Time和 PowerMock 的情况下正确实现它?
ZonedDateTime我正在使用 JDK 8,并且Timestamp经常使用它。但我仍然无法解决我面临的问题。
假设我得到了TimestampGMT (UTC) 格式的文件,并且我的服务器位于某处。假设它设置为Asia/CalcuttaTimeZone (其 ZoneOffset 为+05:30)。
基本上如何在时间戳中添加/减去我的时区偏移量?
Input : 2017-09-13 13:10:30.333
Output: 2017-09-13 18:40:30.333
Run Code Online (Sandbox Code Playgroud)
解释 :在输入中,时区偏移量 5hrs 30mts 已添加到输入中。此外,输入是 Timestamp 类型,输出也是 Timestamp 类型。
我使用ZoneID.systemDefault()函数来检索 JVM 已知的时区。就我而言,时区结果是Asia/Calcutta.
使用'old',Java 8之前SimpleDateFormat我可以做到:
new java.text.SimpleDateFormat("MMM yyyy", new java.util.Locale("es", "ES")).parse("Mayo 2017")
Run Code Online (Sandbox Code Playgroud)
获取Date具有西班牙月份名称的日期对象.
如何使用Java 8实现相同的功能DateTimeFormatter?
我试过了:
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).withLocale(new Locale("es", "ES")).ofPattern("MMM yyyy").parse("Mayo 2017")
Run Code Online (Sandbox Code Playgroud)
但只得到一个java.time.format.DateTimeParseException.
我的 Java Bean 中有一个字段,如下所示,其中@JsonFormat包含 jackson 注释:
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm")
private Timestamp createdOn;
Run Code Online (Sandbox Code Playgroud)
当我返回 时Timestamp,它总是在我的 JSON 响应中转换为 UTC 时间。让我们说格式化我的时间戳,我得到2017-09-13 15:30但我的响应被返回为2017-09-13 10:00.
我知道这是因为 Jackson 注释默认采用 System TimeZone 并将时间戳转换为 UTC。(在我的情况下,服务器属于Asia/Calcutta时区,因此偏移量是+05:30,杰克逊映射器减去 5 小时 30 分钟以将时间戳转换为 UTC)
有没有办法按原样返回时间戳,即,2017-09-13 15:30而不是2017-09-13 10:00?
我有一个String类似的日期2017-09-16T05:06:18.157,我想将其转换为当地时间 (IST)。在印度标准时间,它将在2017-09-16 10:36:18。
使用 Joda-Time,我尝试将其转换为本地,但我无法做到。
下面是我的代码:
private String getConvertDate(String date_server) {
DateTimeFormatter inputFormatter = DateTimeFormat
.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS")
.withLocale(Locale.US);
DateTime parsed = inputFormatter.parseDateTime(date_server);
DateTimeFormatter outputFormatter = DateTimeFormat
.forPattern("yyyy-MM-dd HH:mm:ss")
.withLocale(Locale.US)
.withZone(DateTimeZone.getDefault());
return outputFormatter.print(parsed);
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试对使用java.time.LocalDateTime. 我能够让模拟工作,但是当我增加时间(分钟或天)时,我最终会得到一个null值。
@RunWith(PowerMockRunner.class)
@PrepareForTest({ LocalDateTime.class })
public class LocalDateTimeMockTest
{
@Test
public void shouldCorrectlyCalculateTimeout()
{
// arrange
PowerMockito.mockStatic(LocalDateTime.class);
LocalDateTime fixedPointInTime = LocalDateTime.of(2017, 9, 11, 21, 28, 47);
BDDMockito.given(LocalDateTime.now()).willReturn(fixedPointInTime);
// act
LocalDateTime fixedTomorrow = LocalDateTime.now().plusDays(1); //shouldn't this have a NPE?
// assert
Assert.assertTrue(LocalDateTime.now() == fixedPointInTime); //Edit - both are Null
Assert.assertNotNull(fixedTomorrow); //Test fails here
Assert.assertEquals(12, fixedTomorrow.getDayOfMonth());
}
}
Run Code Online (Sandbox Code Playgroud)
我明白(我想我明白)这LocalDateTime是不可变的,我认为我应该得到一个新实例而不是null值。
原来是.of方法给了我一个null价值。为什么?