【发布时间】:2014-05-01 07:35:04
【问题描述】:
我想将日期从一个时区转换为另一个。 java.util.Date 已经有问题了, 所以按照人们的建议尝试使用 JODA-
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
public class JodaTimeTest {
public static void main(String[] args) throws ParseException {
DateFormat formatter = new SimpleDateFormat("dd-MMM-yy hh:mm:ss a");
System.out.println(getDateStringToShow(formatter.parse("26-Nov-10 03:31:20 PM +0530"), "Asia/Calcutta", "Europe/Dublin", false));
System.out.println(getDateStringToShow(formatter.parse("02-Oct-10 10:00:00 AM +0530"), "Asia/Calcutta", "Europe/Dublin", false));
System.out.println(getDateStringToShow(formatter.parse("26-Nov-10 11:51:20 PM +0530"), "Asia/Kolkata", "Europe/Dublin", false));
System.out.println(getDateStringToShow(formatter.parse("02-Oct-10 01:01:00 AM +0530"), "Asia/Kolkata", "Europe/Dublin", false));
}
public static String getDateStringToShow(Date date, String sourceTimeZoneId, String targetTimeZoneId, boolean includeTime) {
DateTime dateRes = new DateTime(date);
DateTime nowIndia = dateRes.toDateTime(DateTimeZone.forID(sourceTimeZoneId));
DateTime nowDub = nowIndia.toDateTime(DateTimeZone.forID(targetTimeZoneId));
DateTimeFormatter fmt = DateTimeFormat.forPattern("dd-MMM-yy z");
System.out.println("nowIndia : " + fmt.print(nowIndia));
System.out.println("nowDub : " + fmt.print(nowDub));
return "---next---";
}
}
运行时-
nowIndia : 26-Nov-10 IST
nowDub : 26-Nov-10 GMT
---next---
nowIndia : 02-Oct-10 IST
nowDub : 02-Oct-10 IST
---next---
nowIndia : 26-Nov-10 IST
nowDub : 26-Nov-10 GMT
---next---
nowIndia : 02-Oct-10 IST
nowDub : 01-Oct-10 IST
---next---
谁能告诉我为什么它显示日期为 IST-02-Oct-10 10:00:00 AM +0530
【问题讨论】:
标签: java datetime timezone jodatime