Java 解析日期以删除时区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22217422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:23:37  来源:igfitidea点击:

Parsing date to remove timezone

javadatesimpledateformat

提问by user3164187

I use the following SimpleDateFormatto parse a string,

我使用以下SimpleDateFormat来解析字符串,

 SimpleDateFormat ft= new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy");
 String notimeZone = ft.format(startDateTime);
 Date date = ft.parse(notimeZone);

startDateTimeis Date object with value in format "Thu Mar 06 12:27:55 IST 2014".

startDateTime是日期对象,其值格式为“Thu Mar 06 12:27:55 IST 2014”。

in notimeZonevariable i get, Thu Mar 06 12:27:55 2014

notimeZone变量中,我得到了,2014 年 3 月 6 日星期四 12:27:55

I am expecting output in variable dateas, Thu Mar 06 12:27:55 2014

我期待可变日期的输出,2014 年 3 月 6 日星期四 12:27:55

But am getting same , Thu Mar 06 12:27:55 IST 2014.

但是我变得一样了,星期四 3 月 6 日 12:27:55 IST 2014。

How to remove the time zone from the date object.

如何从日期对象中删除时区。

Please help.

请帮忙。

回答by RuntimeException

java.util.Date doesnot have a Timezone. It is not aware of TimeZone.

java.util.Date does没有一个Timezone。它不知道TimeZone.

When you print, java picks up the default time zone.

打印时,java 会选择默认时区。

Conceptually, you cannot have a date time without timezone. A date time has to be in one and only one zone. You may convert it to other zones, but it can never be without a zone.

从概念上讲,没有时区就不能有日期时间。日期时间必须在一个且只有一个区域中。您可以将其转换为其他区域,但它永远不会没有区域。

If your business use case requires awareness of Time Zone, I would prefer to use Calendarclass, or Joda Time. Avoid playing with Date class. If your business use cases do not require any time zone awareness, then go ahead with date. Assume all Date instances are in your default time zone.

如果您的业务用例需要时区意识,我更愿意使用Calendarclass 或Joda Time. 避免玩 Date 类。如果您的业务用例不需要任何时区意识,请继续使用日期。假设所有 Date 实例都在您的默认时区。

Best thing is to use Calendar(or Joda Time). Calendaris a bit unintuitive but you will be able to get your job done. It is not without reason that Date class is mostly deprecated.

最好的办法是使用Calendar(或Joda Time)。Calendar有点不直观,但您将能够完成工作。不推荐使用 Date 类并非没有理由。

Initialize your Calendarwith Timezone(check out available constructors). Use Dateformatto convert and show in UIin whatever time zone you wish to. I posted some code in your previous question. That might help you. I was stuck in a similar problem. I have dates in the DB in one time zone. To display them in another time zone on UI, you need to convert that datetime to another zone.

初始化CalendarTimezone(检查可用的构造函数)。用于DateformatUI您希望的任何时区转换和显示。我在您之前的问题中发布了一些代码。那可能对你有帮助。我陷入了类似的问题。我在一个时区的数据库中有日期。要在 UI 上的另一个时区显示它们,您需要将该日期时间转换为另一个时区。

回答by sumanr

The following format gives

以下格式给出

"EEE MMM dd HH:mm:ss z yyyy"->Thu Mar 06 13:18:02 IST 2014.

“EEE MMM dd HH:mm:ss z yyyy”-> Thu Mar 06 13:18:02 IST 2014。

"EEE MMM dd HH:mm:ss yyyy" -> Thu Mar 06 13:18:02 2014.

“EEE MMM dd HH:mm:ss yyyy”-> 2014 年 3 月 6 日星期四 13:18:02。

Check if you gave a 'z' by mistake in the date format string. For more details, Refer - http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

检查您是否在日期格式字符串中错误地给出了 'z'。有关更多详细信息,请参阅 - http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html