本文介绍了从Date转换为DateTime时如何删除时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从数据库获取的日期,我想将其转换为DateTime并将其设置为开始日期.我写在下面:

I have a Date get from Database and I want convert it to DateTime and set it to begin day. I write it below:

Date fromDate = studentDateJoin.getDate();
//result: fromDate = Mon Jan 01 00:00:02 ICT 2018
//convert to DateTime
DateTime newDate = new DateTime(fromDate);

但是当我转换它时,请在转换后添加时区. newDate看起来像:

But when I convert it add time zone after convert. newDate look like :

newDate = 2018-01-01T14:13:04.574Z+7

我有一个问题:如何将其转换为开始日期并删除时区:示例我想将其转换为:

I have a question: How to convert it to begin day and remove timezone: Example I want to convert it look like:

 //Convert it begin day and remove time zone
 newDate = 2018-01-01T00:00:00.00;

如何将其转换为beginDay并删除时区:谢谢

How to convert it to beginDay and remove timezone: Thankyou

推荐答案

更新:添加了Joda-Time版本,因为问题已从java-8更改为jodatime.

UPDATE: Added Joda-Time version, since question was changed from java-8 to jodatime.

如果您有Joda-Time DateTime,请使用 toLocalDate() toLocalDateTime(...) :

If you have a Joda-Time DateTime, then use toLocalDate() and toLocalDateTime(...):

DateTime dateTime = new DateTime(2018, 1, 1, 14, 13, 04, 574);
System.out.println(dateTime);
LocalDate localDate = dateTime.toLocalDate();
System.out.println(localDate);
LocalDateTime localDateTime = localDate.toLocalDateTime(LocalTime.MIDNIGHT);
System.out.println(localDateTime);
2018-01-01T14:13:04.574-05:00
2018-01-01
2018-01-01T00:00:00.000


如果您有从数据库获取日期",即java.sql.Date,请使用 toLocalDate() atStartOfDay() :


If you have "a Date get from Database", i.e. a java.sql.Date, then use toLocalDate() and atStartOfDay():

java.sql.Date sqlDate = new java.sql.Date(118, 0, 1);
System.out.println(sqlDate);
LocalDate localDate = sqlDate.toLocalDate();
System.out.println(localDate);
LocalDateTime localDateTime = localDate.atStartOfDay();
System.out.println(localDateTime);
2018-01-01
2018-01-01
2018-01-01T00:00


如果您有java.sql.Timestamp,请使用 toLocalDateTime() truncatedTo(...) :


If you have a java.sql.Timestamp, use toLocalDateTime() and truncatedTo(...):

java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(118, 0, 1, 14, 13, 04, 574000000);
System.out.println(sqlTimestamp);
LocalDateTime localDateTime = sqlTimestamp.toLocalDateTime();
System.out.println(localDateTime);
localDateTime = localDateTime.truncatedTo(ChronoUnit.DAYS);
System.out.println(localDateTime);
2018-01-01 14:13:04.574
2018-01-01T14:13:04.574
2018-01-01T00:00


如果您有java.util.Date,请使用 toInstant() atZone(...) toLocalDate() atStartOfDay() :


If you have a java.util.Date, use toInstant(), atZone(...), toLocalDate(), and atStartOfDay():

java.util.Date utilDate = new java.util.Date(118, 0, 1, 14, 13, 04);
System.out.println(utilDate);
Instant instant = utilDate.toInstant();
System.out.println(instant);
ZonedDateTime zonedDateTime = instant.atZone(ZoneId.systemDefault());
System.out.println(zonedDateTime);
LocalDateTime localDateTime = zonedDateTime.toLocalDate().atStartOfDay();
System.out.println(localDateTime);
Mon Jan 01 14:13:04 EST 2018
2018-01-01T19:13:04Z
2018-01-01T14:13:04-05:00[America/New_York]
2018-01-01T00:00

这篇关于从Date转换为DateTime时如何删除时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-08 09:43