我想将格式为MM/dd/yyyy HH:mm
的{{3}}转换为PST Time。
我试过的代码如下。目前我在IST Time Zone
并使用JDK1.7
。
以下代码生成输出This is the Code Test : 08/31/2015 07:20 MST
我怀疑它是否真的是MST!因为MST Time在这里显示为20:50
所以我教过它,它应该显示为8:50
,考虑到am / pm(12hr)时钟。
这让我很困惑 - 我想知道出了什么问题?
/**
*
* Read a Date in the Format of MM/dd/yyyy HH:mm which is a (PST) Time Zone.
* Convert it to MST Time Zone and Save.
*
* Step 1 : Set Timezone as PST to Date in format of MM/dd/yyyy HH:mm,
* Step 2 : Convert PST to MST Time,
* Step 3 : Convert MST Time to Date in format of MM/dd/yyyy HH:mm.
*
* Eg: PST Time Now -> 08/31/2015 19:50;
* MST Time Now -> 08/31/2015 20:50;
*
*
*/
package dates;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
public class DateUtilities {
public static void main(String[] args) {
String dateInString = "08/31/2015 19:50";
System.out.println("This is the Code Test"+MstTimeNow(step1(StringToDate(dateInString))));
// step2(step1(StringToDate(dateInString)));
}
// String To Date
public static Date StringToDate(String dateInString) {
Date date = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm");
date = sdf.parse(dateInString);
// System.out.println("String To Date "+date);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
// To Set TimeZone as PST
public static Calendar step1(Date date) {
Calendar cal = Calendar.getInstance();
TimeZone tz = TimeZone.getTimeZone("PST");
cal.setTimeZone(tz);
cal.setTime(date);
// System.out.println("Get Calendar PST Time: "+cal.getTime() +" TimeZone "+cal.getTimeZone());
return cal;
}
// Calendar To MST Time
public static String MstTimeNow(Calendar cal) {
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy HH:mm z");
formatter.setCalendar(cal);
formatter.setTimeZone(TimeZone.getTimeZone("MST"));
String mstTimeString = formatter.format(cal.getTime());
// System.out.println("MST time Now is : "+mstTimeString);
return mstTimeString;
}
}
由MST Time给出的Solution产生的输出低于预期值!
Input String : 08/31/2015 19:50
Local Date Time: 2015-08-31T19:50
Zoned Date Time PST: 2015-08-31T19:50-07:00[America/Los_Angeles]
Zoned Date Time MST: 2015-08-31T20:50-06:00[America/Denver]
答案 0 :(得分:0)
如果您可以使用新的Java时间API,您的代码可以更简单:
String input = "08/31/2015 19:50";
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm")
LocalDateTime ldt = LocalDateTime.parse(input, fmt);
ZonedDateTime pst = ZonedDateTime.of(ldt, ZoneId.of("America/Los_Angeles"));
ZonedDateTime mst = pst.withZoneSameInstant(ZoneId.of("America/Denver"));
答案 1 :(得分:0)
我在Java1.7
中尝试了以下代码,以下代码仅是从PST转换为MST的一部分。 :P
public static Date convertPstToMst(String dateInString){
Date mstDate = null;
// Set Default TimeZone as PST
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm");
sdf.setTimeZone(TimeZone.getTimeZone(PST_TIMEZONE));
try {
Calendar pstCal = Calendar.getInstance(TimeZone.getTimeZone(PST_TIMEZONE));
pstCal.setTime(sdf.parse(dateInString));
String pstTime = ((pstCal.get(Calendar.MONTH)+1)+"/"+pstCal.get(Calendar.DATE)+"/"+pstCal.get(Calendar.YEAR)+" "+pstCal.get(Calendar.HOUR_OF_DAY)+":"+pstCal.get(Calendar.MINUTE));
System.out.println(pstTime);
pstCal.setTimeZone(TimeZone.getTimeZone(MST_TIMEZONE));
String mstTime = ((pstCal.get(Calendar.MONTH)+1)+"/"+pstCal.get(Calendar.DATE)+"/"+pstCal.get(Calendar.YEAR)+" "+pstCal.get(Calendar.HOUR_OF_DAY)+":"+pstCal.get(Calendar.MINUTE));
System.out.println(mstTime);
// Converting String to Date
mstDate = convertStringToDate(mstTime);
} catch (ParseException e) {
e.printStackTrace();
}
return mstDate;
}