I have a string like this 2013-10-22T01:37:56
. I Need to change this string into UTC Date format like this MM/dd/yyyy KK:mm:ss a
. I have tried some code but it is not returning the UTC datetime.
我有一个像这样的字符串:2013-10-22T01:37:56。我需要将这个字符串转换为UTC日期格式,就像这个MM/dd/ yyyyy KK: MM:ss a。我试过一些代码,但是它不返回UTC日期时间。
My code is
我的代码是
String[] time = itsAlarmDttm.split("T");
String aFormatDate = time[0]+ " "+time[1];
String aRevisedDate = null;
try {
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
final Date dateObj = sdf.parse(aFormatDate);
aRevisedDate = new SimpleDateFormat("MM/dd/yyyy KK:mm:ss a").format(dateObj);
System.out.println(aRevisedDate);
} catch (ParseException e) {
itsLogger.error("Error occured in Parsing the Data Time Object: " +e.getMessage());
} catch (Exception e) {
itsLogger.error("Error occured in Data Time Objecct: " +e.getMessage());
}
I am getting the output is MM/dd/yyyy KK:mm:ss a
format. But Not UTC time format. How to solve this issue?
我得到的输出是MM/dd/ yyyyy KK: MM:ss a格式。但不是UTC时间格式。如何解决这个问题?
4 个解决方案
#1
9
Try to format your date with the Z
or z
timezone flags:
尝试使用Z或Z时区标志来格式化您的日期:
new SimpleDateFormat("MM/dd/yyyy KK:mm:ss a Z").format(dateObj);
#2
15
Try this... Worked for me and printed 10/22/2013 01:37:56 AM
Ofcourse this is your code only with little modifications.
试试这个…为我工作并打印了10/22/2013 01:37:56 AM当然这是你的代码,只有很少的修改。
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC")); // This line converts the given date into UTC time zone
final java.util.Date dateObj = sdf.parse("2013-10-22T01:37:56");
aRevisedDate = new SimpleDateFormat("MM/dd/yyyy KK:mm:ss a").format(dateObj);
System.out.println(aRevisedDate);
#3
4
What Time Zones?
No where in your question do you mention time zone. What time zone is implied that input string? What time zone do you want for your output? And, UTC is a time zone (or lack thereof depending on your mindset) not a string format.
你的问题中没有提到时区。输入字符串隐含的时区是什么?你想要什么时区的输出?而且,UTC是一个时区(或者根据你的思维方式是否存在),而不是字符串格式。
ISO 8601
Your input string is in ISO 8601 format, except that it lacks an offset from UTC.
您的输入字符串是ISO 8601格式,除了缺少UTC的偏移量。
Joda-Time
Here is some example code in Joda-Time 2.3 to show you how to handle time zones. Joda-Time has built-in default formatters for parsing and generating String representations of date-time values.
下面是Joda-Time 2.3中的一些示例代码,向您展示如何处理时区。Joda-Time内置了用于解析和生成日期-时间值的字符串表示形式的默认格式器。
String input = "2013-10-22T01:37:56";
DateTime dateTimeUtc = new DateTime( input, DateTimeZone.UTC );
DateTime dateTimeMontréal = dateTimeUtc.withZone( DateTimeZone.forID( "America/Montreal" );
String output = dateTimeMontréal.toString();
As for generating string representations in other formats, search * for "Joda format".
对于生成其他格式的字符串表示,搜索*以获取“Joda格式”。
#4
3
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
// or SimpleDateFormat sdf = new SimpleDateFormat( "MM/dd/yyyy KK:mm:ss a Z" );
sdf.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
System.out.println( sdf.format( new Date() ) );
#1
9
Try to format your date with the Z
or z
timezone flags:
尝试使用Z或Z时区标志来格式化您的日期:
new SimpleDateFormat("MM/dd/yyyy KK:mm:ss a Z").format(dateObj);
#2
15
Try this... Worked for me and printed 10/22/2013 01:37:56 AM
Ofcourse this is your code only with little modifications.
试试这个…为我工作并打印了10/22/2013 01:37:56 AM当然这是你的代码,只有很少的修改。
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC")); // This line converts the given date into UTC time zone
final java.util.Date dateObj = sdf.parse("2013-10-22T01:37:56");
aRevisedDate = new SimpleDateFormat("MM/dd/yyyy KK:mm:ss a").format(dateObj);
System.out.println(aRevisedDate);
#3
4
What Time Zones?
No where in your question do you mention time zone. What time zone is implied that input string? What time zone do you want for your output? And, UTC is a time zone (or lack thereof depending on your mindset) not a string format.
你的问题中没有提到时区。输入字符串隐含的时区是什么?你想要什么时区的输出?而且,UTC是一个时区(或者根据你的思维方式是否存在),而不是字符串格式。
ISO 8601
Your input string is in ISO 8601 format, except that it lacks an offset from UTC.
您的输入字符串是ISO 8601格式,除了缺少UTC的偏移量。
Joda-Time
Here is some example code in Joda-Time 2.3 to show you how to handle time zones. Joda-Time has built-in default formatters for parsing and generating String representations of date-time values.
下面是Joda-Time 2.3中的一些示例代码,向您展示如何处理时区。Joda-Time内置了用于解析和生成日期-时间值的字符串表示形式的默认格式器。
String input = "2013-10-22T01:37:56";
DateTime dateTimeUtc = new DateTime( input, DateTimeZone.UTC );
DateTime dateTimeMontréal = dateTimeUtc.withZone( DateTimeZone.forID( "America/Montreal" );
String output = dateTimeMontréal.toString();
As for generating string representations in other formats, search * for "Joda format".
对于生成其他格式的字符串表示,搜索*以获取“Joda格式”。
#4
3
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
// or SimpleDateFormat sdf = new SimpleDateFormat( "MM/dd/yyyy KK:mm:ss a Z" );
sdf.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
System.out.println( sdf.format( new Date() ) );