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));
}
}