1. java中如何獲得當前時間並輸出:時,分,秒,
import java.util.*;
import java.text.*;
public class TimeTest {
///詳細見java.util.SimpleDateFormat;
///java.util.Calendar;
//裡面有get(xxFIELD);方法很方便就獲取了時,分,秒,毫秒等數值
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");//時:分:秒.毫秒
GregorianCalendar gc = new GregorianCalendar();
System.out.println(sdf.format(gc.getTime()));
////////////////增加2分鍾
gc.add(GregorianCalendar.MINUTE, 2);
System.out.println(sdf.format(gc.getTime()));
}
}
2. 在java裡面,如何得到UTC時間, 時間格式為:Tue Oct 12 00:00:00 UTC 0800 2010
Calendar gc = GregorianCalendar.getInstance();
cal.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset));
//之後調用cal.get(int x)或cal.getTimeInMillis()方法所取得的時間即是UTC標准時間。
System.out.println("UTC:"+new Date(cal.getTimeInMillis()));
贈送其它時間方法,總有一款適合您
public static void main(String[] args) {
SimpleDateFormat foo = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println("foo:"+foo.format(new Date()));
Calendar gc = GregorianCalendar.getInstance();
System.out.println("gc.getTime():"+gc.getTime());
System.out.println("gc.getTimeInMillis():"+new Date(gc.getTimeInMillis()));
//當前系統默認時區的時間:
Calendar calendar=new GregorianCalendar();
System.out.print("時區:"+calendar.getTimeZone().getID()+" ");
System.out.println("時間:"+calendar.get(Calendar.HOUR_OF_DAY)+":"+calendar.get(Calendar.MINUTE));
//美國洛杉磯時區
TimeZone tz=TimeZone.getTimeZone("America/Los_Angeles");
//時區轉換
calendar.setTimeZone(tz);
System.out.print("時區:"+calendar.getTimeZone().getID()+" ");
System.out.println("時間:"+calendar.get(Calendar.HOUR_OF_DAY)+":"+calendar.get(Calendar.MINUTE));
Date time=new Date();
//1、取得本地時間:
java.util.Calendar cal = java.util.Calendar.getInstance();
//2、取得時間偏移量:
int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET);
//3、取得夏令時差:
int dstOffset = cal.get(java.util.Calendar.DST_OFFSET);
//4、從本地時間里扣除這些差量,即可以取得UTC時間:
cal.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset));
//之後調用cal.get(int x)或cal.getTimeInMillis()方法所取得的時間即是UTC標准時間。
System.out.println("UTC:"+new Date(cal.getTimeInMillis()));
Calendar calendar1 = Calendar.getInstance();
TimeZone tztz = TimeZone.getTimeZone("GMT");
calendar1.setTimeZone(tztz);
System.out.println(calendar.getTime());
System.out.println(calendar.getTimeInMillis());
}
運算結果是Tue Oct 19 16:54:57 CST 2010 符合你的要求
只是以毫秒來算的