简体   繁体   English

如何在Java中将日期和时间组合转换为天

[英]How to convert Date and Time Combination into Days in Java

I have Date and Time combination as 2012-02-28T13:10:50Z which I got from the WebService. 我有从WebService获得的Date and Time组合作为2012-02-28T13:10:50Z

I need to convert that String into number of Days. 我需要将该字符串转换为天数。

您可以使用SimpleDateFormat解析时间戳,并通过除以24*60*60*1000L >自1970年1月1日以来的天数,将毫秒数Date.getTime()转换为天。

You may find this helpful, this is specific to the XML Schema date/time: 您可能会发现这很有用,这特定于XML模式日期/时间:

DatatypeFactory.newXMLGregorianCalendar

Follow the link on that page to the class XMLGregorianCalendar for details on how to extract all components. 单击该页面上的链接到XMLGregorianCalendar类,以获取有关如何提取所有组件的详细信息。

To parse the date, use 要解析日期,请使用

import java.text.SimpleDateFormat;

SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss'Z'");

I'm not sure about the trailing Z. 我不确定尾随的Z。

date = sdf.parse (d);
// java.util.Date = Tue Feb 28 13:10:50 CET 2012

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM