Java中描述日期时间调用Date类型,该类型存储有时间的年月日、时分秒信息。
Date系统时间是以 1970年0时0分0秒 开始计时。
其中:
需要获取的时间单位 | 格式 |
---|---|
年 | YYYY or yyyy |
月 | MM(大写) |
日 | dd(小写) |
时 | HH or hh |
分 | mm(小写) |
秒 | ss(小写) |
毫秒 | SS(大写) |
从当年的1月1日起,到获取时间是该年份的第几天 | DD(大写) |
方法一:Calendar类
import ;
import ;
public class Demo {
public static void main(String[] args) {
Calendar calendar = ();
(new Date()); // 放入Date类型数据
(); // 获取年份
(); // 获取月份
(); // 获取日
(); // 时(12进制)
(Calendar.HOUR_OF_DAY); // 时(24进制)
(); // 分
(); // 秒
(); // 毫秒
(());
(());
(());
(());
((Calendar.HOUR_OF_DAY));
(());
(());
(());
}
}
方法二:SimpleDateFormat类
import ;
import ;
import ;
public class Demo {
public static void main(String[] args) {
File file = new File("D:/time/");
// 获取在D盘下time文件夹里面最后一次修改时间
long lastModified = ();
// 此处显示单位秒(1970年1月1日为绝对时间时刻,到文件修改的最后时刻共经过多少秒)
(lastModified);
// 获取年份
SimpleDateFormat year = new SimpleDateFormat("YYYY");
String format1 = (new Date(lastModified));
// 获取月份
SimpleDateFormat month = new SimpleDateFormat("MM");
String format2 = (new Date(lastModified));
// 获取日
SimpleDateFormat date = new SimpleDateFormat("dd");
String format3 = (new Date(lastModified));
// 时
SimpleDateFormat hour = new SimpleDateFormat("HH");
String format4 = (new Date(lastModified));
// 分
SimpleDateFormat minute = new SimpleDateFormat("mm");
String format5 = (new Date(lastModified));
// 秒
SimpleDateFormat second = new SimpleDateFormat("ss");
String format6 = (new Date(lastModified));
// 毫秒
SimpleDateFormat milliSecond = new SimpleDateFormat("SS");
String format7 = (new Date(lastModified));
//该年份从1月1日为绝对是件,到文件修改的最后时刻共经过多少天
SimpleDateFormat numberDay = new SimpleDateFormat("DD");
String format8 = (new Date(lastModified));
(format1);
(format2);
(format3);
(format4);
(format5);
(format6);
(format7);
(format8);
}
}