20201012冯强
学习总结:
1.Date
1.1java.util.Date
表示特定的时间,用来表示时间和日期,提供一系列操作。
public class Date implements java.io.Serializable, Cloneable, Comparable<Date>
获取的是系统的时间和日期
1天= 24×60×60 = 86400秒
计算机标准是根据Greenwich(格林威治)标准时间(GMT),由于中国属于东八区,所以要比 GMT
时间早8个小时
类的所有方法 Date 接受或返回年,月,日,小时,分钟和秒值,以下表述中使用:
y 年代表整数 y - 1900 。
一个月由0到11的整数表示; 0是1月,1是2月,等等,11是12月
日期(月的一天)以通常的方式从1到31的整数表示。
一小时由0到23之间的整数表示。因此,从午夜到凌晨1点的时间是小时0,从中午到下午1点的小
时是12小时。
一般以0〜59的整数表示 minute 。
秒由0到61的整数表示; 值60和61仅发生在闰秒上,甚至仅在实际上正确跟踪闰秒的Java实现中发
生。 由于目前引入闰秒的方式,在同一分钟内不会发生两个闰秒,但是本规范遵循ISO C的日期
和时间约定。
在所有情况下,为这些目的而提供的方法的论证不必在指定范围内; 例如,可以将日期指定为1月32日,
并将其解释为2月1日
1.2成员变量
private transient long fastTime;//保存Date所表示的时间
1.3成员方法
public static void main(String[] args) {
Date date = new Date(); System.out.println(date);
//Mon Oct 12 10:28:54 CST 2020
Date date1 = new Date(1000L); System.out.println(date1);
//Thu Jan 01 08:00:01 CST 1970
}
2.Calendar
2.1java.util.Calendar
一个抽象类。可以为在某一特定时刻和日历字段之间的转换的方法,以及用于
操纵该日历的字段提供了方法, 时间上的瞬间可以用毫秒值表示,该值是从1970年1月1日00:00 00:
00.000 GMT
public abstract class Calendar implements Serializable, Cloneable, Comparable<Calendar>
Calendar.getInstance();//不能实例化所以提供一个方法返回一个子类对象
2.2Date和Calendar相互转换
public static void main(String[] args) { Calendar calendar = new GregorianCalendar(); // java.util.Date 类中的 getTime() 方法可以返回一个毫秒值 // java.util.Calendar 类中的 getTime() 用于返回相应的 Date 实例
Date d = calendar.getTime(); System.out.println(d);
//Thu Oct 24 20:18:49 CST 2019
Date date = new Date(); Calendar c = Calendar.getInstance(); c.clear(); //清空所有字段值
c.setTime(date);
//设置时间
System.out.println(c);
}
}
3.DateFormat
3.1将字符串解析为Date对象
public static void main(String[] args) {
//重点:将 指定格式字符串 解析为 java.util.Date 对象 final String pattern = "yyyy-MM-dd";//字符串格式
SimpleDateFormat sdf = new SimpleDateFormat(pattern); Scanner sc = new Scanner(System.in);
// 创建扫描器读取用户输入的数据 System.out.println("请输入一个日期( 格式为 " + pattern + " ,比如 2019- 10-24 )");String s = sc.nextLine(); // 读取用户输入的整行数据
System.out.println("你输入的是: " + s); //你输入的是: 2019-10-10
try {// 将 字符串 解析为 Date 类型的实例 Date date = sdf.parse(s); //如果格式不正确,可能抛出 ParseException 异常 System.out.println(date);//Thu Oct 10 00:00:00 CST 2019
long time = date.getTime();//1570636800000
System.out.println(time);
} catch (ParseException e) {
System.out.println("你输入的日期格式不符合 " + pattern);
e.printStackTrace(); }sc.close(); // 关闭扫描器
}
}
4.LocalDateTime
LocalDateTime和日期转换
public static void main(String[] args) {
Date date =new Date();
Instant in =date.toInstant();
ZonedDateTime zt =in.atZone(ZoneId.systemDefault());
LocalDate lc =zt.toLocalDate();
LocalTime ll= zt.toLocalTime();
System.out.println(ll);
System.out.println(lc);
LocalDateTime lcs = lc.atStartOfDay();
ZonedDateTime z =lcs.atZone(ZoneId.systemDefault());
Instant s =z.toInstant();
Date date1 = Date.from(s);
System.out.println(date1);
}
5.File类
查看某个文件下所有文件的大小
public class Demo2 {
private static Long byteSum=0L;
public static void main(String[] args) {
long sum = selectFileList("D:\\QQ");
System.out.println(sum);
}
public static long selectFileList(String filepath) {
File file = new File(filepath);
if(file.isDirectory()) {
String[] fileList = file.list();
for (int i = 0; i < fileList.length; i++) {
File childFile = new File(filepath+"\\"+fileList[i]);
if(childFile.isDirectory()) {
selectFileList(childFile.getPath());
}else{
byteSum+=childFile.length();
}
//childFile.isDirectory()?selectFileList(childFile.getPath()):byteSum+=childFile.length();
}
return byteSum;
}else {
throw new RuntimeException("你输入的路径不是文件夹或不存在");
}
}
}
近期评论