简体   繁体   English

SimpleDateFormat解析不以所需格式返回日期

[英]SimpleDateFormat Parse not returning Date in required Format

Please find my below Code what is does is it takes the TimeStamp Value of any timeZone Converts it to required TimeZone and gives you the Date of requiredTime Zone . 请在下面的代码中找到我的代码,该代码将获取任何timeZone的TimeStamp值并将其转换为所需的TimeZone并为您提供requiredTime Zone的日期。 It works the date variable is coming correctly but I need the value in (Date) Datatype so am parsing it back using the same SimpleDataFormat object but its returning me a value in different Format not the one mentioned in the SimpleDataFormat Object . 它可以正常运行日期变量,但我需要(Date)数据类型中的值,因此使用相同的SimpleDataFormat对象将其解析回去,但它以不同的Format返回我的值,而不是SimpleDataFormat对象中提到的值。

private Date getDateOfTimeZone(Timestamp timeStamp, String timeZoneCode)
        throws ParseException {
    SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MM-yy");
    DATE_FORMAT.setTimeZone(TimeZone.getTimeZone(timeZoneCode));
    String date = DATE_FORMAT.format(timeStamp);
    return DATE_FORMAT.parse(date);
}

Input Varibles : TimeStamp : 2013-11-01 16:19:37.0 , TimeZone : "IST"
Date value is coming as  : 02-11-13 (Correct)
But Parse() is returning me  : Fri Nov 01 14:30:00 EDT 2013.

I can see the date is converted according to timeZone but why parse is not returning it in required format ie "dd-MM-yy". 我可以看到日期是根据timeZone转换的,但是为什么解析没有以要求的格式即“ dd-MM-yy”返回它。

The parse method returns a Date. parse方法返回一个Date。

When you print the Date , you get the output from the toString() method which is what you see (Fri Nov 01 14:30:00 EDT 2013) 当您打印Date ,您从toString()方法获得的输出即为您所看到的(Fri Nov 01 14:30:00 EDT 2013)

To print it in the format you would like, you can use the format method to convert it into a String and then print it. 要以所需的格式打印,可以使用format方法将其转换为String,然后进行打印。

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

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