1 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設置日期格式 System.out.println(df.format(new Date()));// new Date()為獲取當前系統時間
2 Calendar c = Calendar.getInstance();//可以對每個時間域單獨修改
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int date = c.get(Calendar.DATE);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
System.out.println(year + "/" + month + "/" + date + " " +hour + ":" +minute + ":" + second);
3 Date nowTime = new Date(System.currentTimeMillis());
SimpleDateFormat
sdFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String
retStrFormatNowDate = sdFormatter.format(nowTime);
❷ JAVA將時分秒格式的時間轉化成秒數
public class TimeToSecond {
public static void main(String[] args) {
String time ="01:22:12";
String[] my =time.split(":");
int hour =Integer.parseInt(my[0]);
int min =Integer.parseInt(my[1]);
int sec =Integer.parseInt(my[2]);
int zong =hour*3600+min*60+sec;
System.out.println("慶拍納散共"+zong+"秒");
}
}
(2)java獲取系統當前時間並轉為秒擴展閱讀
java將毫秒值轉換為譽茄羨日期時間
public static void main(String[] args) {
long milliSecond = 1551798059000L;
Date date = new Date();
date.setTime(milliSecond);
System.out.println(new SimpleDateFormat().format(date));
}
❸ java中如何獲得當前時間並輸出:時,分,秒,
import java.text.*;
import java.util.*;
public class Test {
public static void main(String[] args) {
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");//時:分:秒:毫秒
System.out.println(sdf.format(d));
Long l = d.getTime();//返回自 1970 年 1 月 1 日 00:00:00 GMT 以來此 Date 對象內表示的毫秒數。
l += 100000;//加100000毫秒.
Date d1 = new Date(l);// 分配 Date 對象並初始化此對象,以表示自從標准基準時容間(稱為「歷元(epoch)」,即 1970 年 1 月 1 日 00:00:00 GMT)以來的指定毫秒數。
System.out.println(sdf.format(d1));
}
}