我想使用Java代码获取不同时区的本地时间。根据传递给函数的时区,我需要该时区的本地时间。怎么做到这一点?
答案 0 :(得分:14)
我建议您查看Joda Time,这是标准Java日期和时间API的替代方案(但非常受欢迎):
http://joda-time.sourceforge.net/index.html
使用Joda Time,我认为这就是你的意思:
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
public class TimeZoneDemo {
public static void main(String[] args) {
DateTime now = new DateTime(System.currentTimeMillis(), DateTimeZone.forID("UTC"));
System.out.println("Current time is: " + now);
}
}
您只需要知道相关时区的标准ID,例如UTC。
答案 1 :(得分:12)
java.util.TimeZone tz = java.util.TimeZone.getTimeZone("GMT+1");
java.util.Calendar c = java.util.Calendar.getInstance(tz);
System.out.println(c.get(java.util.Calendar.HOUR_OF_DAY)+":"+c.get(java.util.Calendar.MINUTE)+":"+c.get(java.util.Calendar.SECOND));
答案 2 :(得分:7)
Java 1.8为您提供了包java.time
中的一些新类:
package learning.java8;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import org.junit.Test;
public class JavaTimeLT {
@Test
public void zonedDataTimeExample() {
final ZoneId zoneId = ZoneId.of("Europe/Zurich");
final ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(Instant.now(), zoneId);
System.out.println(zonedDateTime.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
}
}
答案 3 :(得分:4)
我编写了以下程序,以便为所有时区提供时间,看看这是否有帮助......
String[] zoneIds = TimeZone.getAvailableIDs();
for (int i = 0; i < zoneIds.length; i++) {
TimeZone tz = TimeZone.getTimeZone(zoneIds[i]);
System.out.print(tz.getID() + " " + tz.getDisplayName());
Calendar calTZ = new GregorianCalendar(tz);
calTZ.setTimeInMillis(new Date().getTime());
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, calTZ.get(Calendar.YEAR));
cal.set(Calendar.MONTH, calTZ.get(Calendar.MONTH));
cal.set(Calendar.DAY_OF_MONTH, calTZ.get(Calendar.DAY_OF_MONTH));
cal.set(Calendar.HOUR_OF_DAY, calTZ.get(Calendar.HOUR_OF_DAY));
cal.set(Calendar.MINUTE, calTZ.get(Calendar.MINUTE));
cal.set(Calendar.SECOND, calTZ.get(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, calTZ.get(Calendar.MILLISECOND));
System.out.println( " "+cal.getTime());
答案 4 :(得分:2)
检查一下。希望它会有所帮助。
TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
Calendar cal = Calendar.getInstance();
int LocalOffSethrs = (int) ((cal.getTimeZone().getRawOffset()) *(2.77777778 /10000000));
int ChinaOffSethrs = (int) ((tz.getRawOffset()) *(2.77777778 /10000000));
TimeZone tz1 = TimeZone.getTimeZone("US/Central");
String ss =cal.getTimeZone().getDisplayName();
System.out.println("Local Time Zone : " + ss);
System.out.println("China Time : " + tz.getRawOffset());
System.out.println("Local Offset Time from GMT: " + LocalOffSethrs);
System.out.println("China Offset Time from GMT: " + ChinaOffSethrs);
cal.add(Calendar.MILLISECOND,-(cal.getTimeZone().getRawOffset()));
//cal.add(Calendar.HOUR,- LocalOffSethrs);
cal.add(Calendar.MILLISECOND, tz.getRawOffset());
Date dt = new Date(cal.getTimeInMillis());
System.out.println("After adjusting offset Acctual China Time :" + dt);
答案 5 :(得分:1)
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
SimpleDateFormat dateFormat = new SimpleDateFormat();
dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Seoul"));
GregorianCalendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 0);
System.out.println(dateFormat.format( cal.getTime()));
答案 6 :(得分:1)
在Java 8中,您可以使用ZonedDateTime.now(ZoneId zone)
方法:
ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("Asia/Tokyo"));
LocalTime localTime = zonedDateTime.toLocalTime();
System.out.println(localTime);