简体   繁体   English

将Java Date转换为另一个Time as Date格式

[英]Convert Java Date into another Time as Date format

I want to convert date into "indies time". 我想将日期转换为“印度时间”。 Specifically: "Asia/Calcutta". 具体来说:“亚洲/加尔各答”。

Code: 码:

// TODO Auto-generated method stub
Date date=new Date();
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));

String dateString=simpleDateFormat.format(date);

System.out.println("Currect Date : " + dateString);
// This is returning a string and I want a date.

System.out.println("Wrong Output : "+simpleDateFormat.parse(dateString)); 
//This returns a Date but has incorrect result.

Here is the output of the above code: 这是上面代码的输出:

Correct Date : 2013-04-15 15:40:04
Wrong Output : Mon Apr 15 03:10:04 MST 2013

I want the DATE, not the string, but when I retrieve the date, the time is 3:10 , and when I get the string, I get 15:40:04 . 我想要DATE,而不是字符串,但是当我检索日期时,时间是3:10 ,而当我得到字符串时,我得到15:40:04 Why are these not the same? 为什么这些不一样?

parse() parses the date text and constructs the Date object which is a long value. parse()分析日期文本并构造一个Date对象,该对象是一个长值。 The parse() javadoc says parse()javadoc说

The TimeZone value may be overwritten, depending on the given pattern and 
the time zone value in text. Any TimeZone value that has previously been 
set by a call to setTimeZone may need to be restored for further operations.

Your parse Date object is printed by calling the toString() on Date which has printed it in the MST timezone. 通过在Date上调用toString()来打印解析的Date对象,该对象已在MST时区中打印。 If you convert it from MST to IST then you will get the timestamp which you are expecting. 如果将其从MST转换为IST,那么您将获得期望的时间戳。 So, your output is correct. 因此,您的输出是正确的。 All you need to do is format and print your long Date value using the correct timezone. 您所需要做的就是使用正确的时区格式化并打印您的长日期值。

Parse will return a DateObject, so calling : 解析将返回一个DateObject,因此调用:

System.out.println("Wrong Output : "+simpleDateFormat.parse(dateString));

is somewhat similar to : 有点类似于:

Date d1 = simpleDateFormat.parse(dateString);
System.out.println("Wrong Output : "+d1.toString());

Remember parsing a date is just to parse a String to generate a date object, if you want to display it in a certain format, use that date object and call sdf.format on it. 请记住,解析日期只是解析字符串以生成日期对象,如果要以某种格式显示它,请使用该日期对象并对其调用sdf.format。 eg 例如

 String dateStringOut=simpleDateFormat.format(d1);
 System.out.println("Output : "+dateStringOut);

A Date does not contain any formatting. Date不包含任何格式。 It is simply a Date . 它只是一个Date You can therefore not convert your Date object between different output formats. 因此,您不能在不同的输出格式之间转换Date对象。 Once you have the date, there's no need trying to convert it into a different format. 确定日期后,就无需尝试将其转换为其他格式。 How to format it is decided when you convert it to a String with your SimpleDateFormat . 如何格式化它,取决于何时使用SimpleDateFormat将其转换为String So once you have parsed your Date , just keep it around until you need to format it for output. 因此,一旦您解析了Date ,就保留它,直到需要格式化它以便输出为止。

Use static function getDateTimeInstance(int dateStyle,int timeStyle) of DateFormat class. 使用DateFormat类的静态函数getDateTimeInstance(int dateStyle,int timeStyle)。 See DateFormat Class for further details. 有关更多详细信息,请参见DateFormat类。 It may help you. 它可能会帮助您。

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

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