常用类:Date类
Date类的常用方法如下:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateDemo01 {
public static void main(String[] args) throws ParseException {
//测试Date类
Date date1 = new Date();
System.out.println(date1);//当前时间 Wed Apr 07 10:11:36 CST 2021
Date date2 = new Date(12345678910L);
System.out.println(date2);//初试时间加上毫秒 Sun May 24 05:21:18 CST 1970
//boolean after(Date when) 测试此日期是否在指定日期之后。
System.out.println(date1.after(date2));//true
//boolean before(Date when) 测试此日期是否在指定日期之前。
System.out.println(date1.before(date2));//false
//int compareTo(Date anotherDate) 比较两个日期。
System.out.println(date1.compareTo(date2));//1
//boolean equals(Object obj) 比较两个相等的日期。
System.out.println(date1.equals(date2));//false
//long getTime() 返回自此 Date对象表示的1970年1月1日00:00:00 GMT以来的毫秒数。
System.out.println(date1.getTime());//1617761676032
//测试SimpleDateFormat类
SimpleDateFormat sdf = new SimpleDateFormat();
//日期对象转字符串
System.out.println(sdf.format(date1));
//字符串转日期对象
System.out.println(sdf.parse("2021/4/7 上午10:19"));
//指定日期格式
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss-SSS");
//转字符串
System.out.println(sdf2.format(date1));
//转日期对象
System.out.println(sdf2.parse("2021-04-07 10-21-30-225"));
}
}
jdk1.8版本后新增了几种日期类API,具体如下:
//测试jdk1.8后新增的日期类API
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
public class DateDemo02 {
public static void main(String[] args) {
//年份
System.out.println("今年"+ Year.now());
System.out.println("指定年" + Year.of(2022));
//月份
System.out.println("月份:"+ Month.FEBRUARY);
System.out.println("月份:"+ Month.of(12));
//今天不包含时分秒
LocalDate today = LocalDate.now();
System.out.println("今天:"+today);
//您的生日
LocalDate yourBirthDate = LocalDate.of(1999, Month.JUNE, 15);
//LocalDate yourBirthDate = LocalDate.of(1999, 6, 15);
System.out.println("生日:"+yourBirthDate);
//此时此刻
LocalDateTime now = LocalDateTime.now();
System.out.println("现在:"+now);
//您的学习时间
LocalDateTime dateTime = LocalDateTime.of(2020, 2, 25, 12, 30,40);
System.out.println("时间:"+dateTime);
//日期转换器
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//格式化日期字符串
LocalDateTime now2 = LocalDateTime.now();
String nowStr = now2.format(formatter); //放入格式器
System.out.println(nowStr);
String dateStr= "2020-02-25 11:23:04";
//转成日期
LocalDateTime date= LocalDateTime.parse(dateStr, formatter);//放入格式器
System.out.println(date);
//获取日
System.out.println(date.getDayOfMonth());
System.out.println(date.getYear());
LocalDate today3 = LocalDate.now();
LocalDate birthDate = LocalDate.of(2001, 4, 7);
//时期间隔 年月日
Period p = Period.between(birthDate, today3);
System.out.printf(p.getYears()+"年"+p.getMonths()+"月"+p.getDays()+"日");
LocalDate startDate = LocalDate.of(1993, 8, 19);
LocalDate endDate = LocalDate.of(1994, Month.JANUARY,16);
//期量单位 间隔
long between = ChronoUnit.YEARS.between(startDate, endDate);
System.out.println("两年之间的差 : " + between); //0 不是1不满一年不计算在内
between =ChronoUnit.MONTHS.between(startDate, endDate);
System.out.println("两月之间的差 : " + between); //4 不是5不满一月不计算在内
//瞬间
//不包含时区 小时: 差8小时
Instant inst1 = Instant.now();
System.out.println(inst1);
Instant inst2 = inst1.minus(Duration.ofSeconds(3));
System.out.println(inst2);
System.out.println("毫秒相隔 : " + Duration.between(inst1, inst2).toMillis());
System.out.println("毫秒相隔 : " + Duration.between(inst1, inst2).toSeconds());
//系统默认时区
ZoneId z = ZoneId.systemDefault();
System.out.println(z);
//static LocalDateTime ofInstant(Instant instant, ZoneId zone)从Instant和区域ID获取LocalDateTime的实例
System.out.println(LocalDateTime.ofInstant(inst1,z));;
System.out.println(inst1);
}
}
常用类:File类
File类常用方法:
import java.io.File;
import java.io.IOException;
public class FileDemo01 {
public static void main(String[] args) throws IOException {
/*
File(File parent, String child) 从父抽象路径名和子路径名字符串创建新的File实例。
File(String pathname) 通过将给定的路径名字符串转换为抽象路径名来创建新的File实例。
File(String parent, String child) 从父路径名字符串和子路径名字符串创建新的File实例。
*/
File file = new File("D://test.txt");
File file2 = new File("D:/hahaha.txt");
File file3 = new File("D:\\test.txt");
File file4 = new File("D://AAA");
File file5 = new File("test.txt");
File file6 = new File(file4,"test.txt");
File file7 = new File("D://AAA","test.txt");
System.out.println(file);
System.out.println(file2);
System.out.println(file3);
System.out.println(file4);
System.out.println(file5);
System.out.println(file6);
System.out.println(file7);
//常用方法
//boolean setReadOnly() 标记此抽象路径名指定的文件或目录,以便仅允许读取操作。
//System.out.println(file.setReadOnly());
//boolean canWrite() 测试应用程序是否可以修改此抽象路径名表示的文件。
//System.out.println(file.canWrite());
//System.out.println(file.canRead());
//System.out.println(file.canExecute());
//boolean createNewFile() 当且仅当具有此名称的文件尚不存在时,以原子方式创建由此抽象路径名命名的新空文件。
//目录需要存在才能创建指定的文件 IOException:系统找不到指定的路径。
System.out.println(file.exists());
if(!file.exists()){
System.out.println(file.createNewFile());
}
//System.out.println(file7.createNewFile());
//boolean exists() 测试此抽象路径名表示的文件或目录是否存在。
//boolean delete() 删除此抽象路径名表示的文件或目录。
System.out.println(file2.delete());
//File getAbsoluteFile() 返回此抽象路径名的绝对形式。
//String getAbsolutePath() 返回此抽象路径名的绝对路径名字符串。
System.out.println(file5.getAbsoluteFile()); //如果想对file路径,默认绝对路径为当前项目所在路径,不包含src
System.out.println(file5.getAbsolutePath());
System.out.println(file5.createNewFile());
}
}
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
public class FileDemo02 {
public static void main(String[] args) throws IOException {
File file = new File("src/haha.txt"); //默认在当前项目下+相对路径
File file2 = new File("/src/haha.txt"); //默认项目在盘符+想对路径
System.out.println(file.getAbsolutePath());
System.out.println(file2.getAbsolutePath());
File file3 = new File("D://");
File file4 = new File("D://AAA/test.txt");
File file5 = new File("D://AAA");
System.out.println(file3.getFreeSpace()); //空暇字节数
//String getName() 返回此抽象路径名表示的文件或目录的名称。
System.out.println(file4.getName());
System.out.println(file5.getName());
//String getParent() 返回此抽象路径名父项的路径名字符串,如果此路径名未指定父目录,则返回 null 。
//File getParentFile() 返回此抽象路径名父项的抽象路径名,如果此路径名未指定父目录,则返回 null 。
System.out.println(file4.getParent());
System.out.println(file4.getParentFile());
//long getTotalSpace() 通过此抽象路径名返回分区 named的大小。
System.out.println(file3.getTotalSpace());
//boolean isAbsolute() 测试此抽象路径名是否为绝对路径。
System.out.println(file5.isAbsolute());
//boolean isDirectory() 测试此抽象路径名表示的文件是否为目录。
//boolean isFile() 测试此抽象路径名表示的文件是否为普通文件。
System.out.println(file5.isDirectory());
System.out.println(file5.isFile());
//long lastModified() 返回上次修改此抽象路径名表示的文件的时间。
System.out.println(new SimpleDateFormat().format(new Date(new File("D://test.txt").lastModified())));;
//long length() 返回此抽象路径名表示的文件的长度。
File f = new File("D://test.txt");
System.out.println(f.length()); //字节数
//String[] list() 返回一个字符串数组,用于命名此抽象路径名表示的目录中的文件和目录。
//File[] listFiles() 返回一个抽象路径名数组,表示此抽象路径名表示的目录中的文件。
System.out.println(Arrays.toString(file5.list()));
System.out.println(Arrays.toString(file5.listFiles()));
//boolean mkdir() 创建此抽象路径名指定的目录。
//boolean mkdirs() 创建此抽象路径名指定的目录,包括任何必需但不存在的父目录。 只能创建目录,不能创建文件
File f2 = new File("D://haha/hehe/heihei.txt");
//System.out.println(f2.mkdirs());
System.out.println(f2.getParentFile().mkdirs());
System.out.println(f2.createNewFile());;
//boolean renameTo(File dest) 重命名此抽象路径名表示的文件。 最好操作同一盘符下 改名|移动
//System.out.println(f.renameTo(new File("D://hehe.txt")));;
System.out.println(new File("D://hehe.txt").renameTo(new File("D://haha/hehe.txt")));;
//练习: 控制台模拟dir命令
}
}