与时间PST转换日期为UTC格式 [英] Converting Date with Time in PST into UTC format
问题描述
我有一个变量STR(字符串型),具有28 - 11月,2013上午09点15分为value.How将其转换为UTC格式(str变量上面提到的时间是在PST,因此UTC应在8小时比)。我现在用下面的柔性2.Find更多的是下面的code这是不工作: -
txtDate.text = formatDateUTC(txtDate.text); //这里txtDate.text = 28 - 11月 - 2013上午09点15分
私有函数formatDateUTC(originalDate:字符串):字符串
{
Alert.show(原始+ originalDate);
VAR dtValue:日期=新的日期(Date.parse(originalDate.replace( - ,)));
VAR editedDate:字符串= pstFormatter.format(dtValue);
Alert.show(编辑+ editedDate);
返回(dateFormatter.format(DATEADD(小时,8,dtValue)))的toString()。
}
私有函数DATEADD(日期部分:字符串=,号码:号码= 0,日期:日期= NULL):日期
{
如果(日期== NULL){
日期=新的日期();
}
VAR returnDate:日期=新的日期(日期);;
开关(datepart.toLowerCase()){
案fullyear:
案月:
案日期:
案小时:
案分钟:
案秒:
案毫秒:
returnDate [日期部分] + =号;
打破;
默认:
/ *未知日期部分,什么也不做。 * /
打破;
}
返回returnDate;
}
聪明的程序员离开繁重的日期时间计算,以一个专业图书馆。在Java中,这将是乔达时(或Java的8,JSR 310)。
下面是Java 7例如$ C $下乔达时2.3。
//©2013罗勒布尔克。此源$ C $ C可以用来自由永远被任何人采取这样做的全部责任。
字符串dateString =28 - 11月 - 2013上午9时15分; //假设为当地的日期时间在美国的西海岸。
//字符串dateString =28 - 11月 - 2013下午9时15分; //测试PM,以及AM如果你喜欢。
//乔达时有pcated,因为他们的不一致使用3个字母的时区codeS的德$ P $。使用其它标识符区。
//时区列表:http://joda-time.sourceforge.net/timezones.html
org.joda.time.DateTimeZone californiaTimeZone = org.joda.time.DateTimeZone.forID(美国/ Los_Angeles);
//乔达 - 时间格式化codeS:http://www.joda.org/joda-time/key_format.html
org.joda.time.format.DateTimeFormatter dateStringFormat = org.joda.time.format.DateTimeFormat.forPattern(DD-MMM-YYYY HH:MM AA).withZone(californiaTimeZone);
org.joda.time.DateTime californiaDateTime = dateStringFormat.parseDateTime(dateString);
org.joda.time.DateTime utcDateTime = californiaDateTime.toDateTime(org.joda.time.DateTimeZone.UTC);
//这两个日期时间的对象重新present同一时刻在时间线宇宙,
//但是presented不同的时区偏移。
的System.out.println(californiaDateTime:+ californiaDateTime);
的System.out.println(utcDateTime:+ utcDateTime);
在运行...
californiaDateTime:2013-11-28T09:15:00.000-08:00
utcDateTime:2013-11-28T17:15:00.000Z
i am having a variable str(string type)having "28-Nov-2013 09:15 AM" as its value.How to convert it into UTC format(the above mentioned time in str variable is in PST, hence UTC should be 8 hours more than that).I am using flex 2.Find below is following code which is not working:-
txtDate.text= formatDateUTC(txtDate.text); //here txtDate.text=28-Nov-2013 09:15 AM
private function formatDateUTC(originalDate:String):String
{
Alert.show('original '+originalDate);
var dtValue:Date = new Date(Date.parse(originalDate.replace("-"," ")));
var editedDate:String=pstFormatter.format(dtValue);
Alert.show('edited '+editedDate);
return (dateFormatter.format(dateAdd("hours",8,dtValue))).toString();
}
private function dateAdd(datepart:String = "", number:Number = 0, date:Date = null):Date
{
if (date == null) {
date = new Date();
}
var returnDate:Date = new Date(date);;
switch (datepart.toLowerCase()) {
case "fullyear":
case "month":
case "date":
case "hours":
case "minutes":
case "seconds":
case "milliseconds":
returnDate[datepart] += number;
break;
default:
/* Unknown date part, do nothing. */
break;
}
return returnDate;
}
Wise programmers leave the heavy-lifting of date-time calculations to a specialized library. In Java, that would be Joda-Time (or in Java 8, JSR 310).
Here is example code for Joda-Time 2.3 in Java 7.
// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
String dateString = "28-Nov-2013 09:15 AM"; // Assumed to be the local date-time in United States west coast.
//String dateString = "28-Nov-2013 09:15 PM"; // Test "PM" as well as "AM" if you like.
// Joda-Time has deprecated use of 3-letter time zone codes because of their inconsistency. Use other identifier for zone.
// Time Zone list: http://joda-time.sourceforge.net/timezones.html
org.joda.time.DateTimeZone californiaTimeZone = org.joda.time.DateTimeZone.forID( "America/Los_Angeles" );
// Joda-Time formatting codes: http://www.joda.org/joda-time/key_format.html
org.joda.time.format.DateTimeFormatter dateStringFormat = org.joda.time.format.DateTimeFormat.forPattern( "dd-MMM-yyyy hh:mm aa" ).withZone( californiaTimeZone );
org.joda.time.DateTime californiaDateTime = dateStringFormat.parseDateTime( dateString );
org.joda.time.DateTime utcDateTime = californiaDateTime.toDateTime( org.joda.time.DateTimeZone.UTC );
// Both of these date-time objects represent the same moment in the time-line of the Universe,
// but presented with different time-zone offsets.
System.out.println( "californiaDateTime: " + californiaDateTime );
System.out.println( "utcDateTime: " + utcDateTime );
When run…
californiaDateTime: 2013-11-28T09:15:00.000-08:00
utcDateTime: 2013-11-28T17:15:00.000Z
这篇关于与时间PST转换日期为UTC格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!