日期相关
Date类
//显示当前时间
Date d1 = new Date();
System.out.println(d1);
//自标准时间1970.1.1的8:00(东八区所以是八点),经过date毫秒的时间
long date = 1000 * 60 * 60;//1000*60*60毫秒等于1小时
Date d2 = new Date(date);
System.out.println(d2);
//获取从标准时间到现在所经历的毫秒数
Date d3 = new Date();
System.out.println(d3.getTime() * 1.0 / 1000 / 60 / 60 / 24 / 365 + "年");//1000*60*60*24*365毫秒等于1年
//显示时间
Date d4 = new Date();
SimpleDateFormat sdf1 = new SimpleDateFormat();//默认格式
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");//设置格式
String s1 = sdf1.format(d4);
System.out.println(s1);//23-1-1 上午10:32
String s2 = sdf2.format(d4);
System.out.println(s2);//2023年01月01日 10:32:45
//解析时间
String s3 = "2003-07-22 00:00:00";
SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d5 = sdf3.parse(s3);
System.out.println(d5);
Calendar类
Calendar c = Calendar.getInstance();//类似于获取对象,其中包含年月日等日期信息(当前)
c.add(Calendar.YEAR, -1);//对象c中的年-1
//也可以自己设置年月日
//c.set(2003,07,22);
//这样获取到的就是设置的值
int year = c.get(Calendar.YEAR);//原本2001变为2000
int month = c.get(Calendar.MONTH) + 1;//注意Calendar中月默认从0开始,所以要加一
int day = c.get(Calendar.DATE);
System.out.println(year + "年" + month + "月" + day + "日");
异常
try......catch......finally
public static void main(String[] args) {
System.out.println("程序开始");
method();
}
private static void method() {
try {
int[] arr = {1, 2, 3};
System.out.println(arr[3]);
} catch (ArrayIndexOutOfBoundsException e) {//三种显示错误的方法
System.out.println(e.getMessage());//3
System.out.println(e);//java.lang.ArrayIndexOutOfBoundsException: 3
e.printStackTrace();//详细信息
} finally {//try...catch后一定会执行finally
System.out.println("程序结束");
}
}
throws
public static void main(String[] args) throws ParseException {//用于处理一些编译时报错的异常,对运行时出错的异常没有用处,仍然需要try...catch
String s = "2003-07-22 00:00:00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = sdf.parse(s);
System.out.println(d);
}
自定义异常
public class scoreException extends Exception {
public scoreException() {
}
public scoreException(String message) {
super(message);
}
}
public static void main(String[] args) throws scoreException {
System.out.println("请输入分数:");
Scanner sc = new Scanner(System.in);
int s = sc.nextInt();
if (s >= 0 && s <= 100) {
System.out.println("成绩合格!");
} else {
throw new scoreException("输入错误!");
}
}
泛型
泛型类
//当需要一个对象可以同时满足多种类型时即可使用泛型类
public class draft {
public static void main(String[] args) {
List<generic> l = new ArrayList<>();
generic<String> g1 = new generic<>("小");
generic<Integer> g2 = new generic<>(100);
l.add(g1);
l.add(g2);
for (generic g : l) {
System.out.println(g.getV());
}
}
}
//T类似于一种参数,没有固定名称
//创建时按正常对象的思路即可
class generic<T> {
private T v;
public generic() {
}
public generic(T v) {
this.v = v;
}
public T getV() {
return v;
}
public void setV(T v) {
this.v = v;
}
}
泛型方法
public class draft {
public static void main(String[] args) {
generic g1=new generic();
g1.show(10);
g1.show("Hello");
g1.show(true);
}
}
class generic {
public <T> void show(T t) {
System.out.println(t);
}
}
泛型接口
public class draft {
public static void main(String[] args) {
generic g1 = new generic();
g1.show(10);
g1.show("Hello");
g1.show(true);
}
}
class generic<T> implements genericImp<T> {
@Override
public void show(T t) {
System.out.println(t);
}
}
interface genericImp<T> {
void show(T t);
}
类型通配符
List<?> l1 = new ArrayList<Object>();//类型通配符,可以是任意一种类型
List<? extends Number> l2 = new ArrayList<Number>();//当和extends搭配时,?可以是extends的类型本身或它的子类
List<? extends Number> l3 = new ArrayList<Integer>();
List<? super Number> l4 = new ArrayList<Object>();//当和super搭配时,?可以是super的类型本身或它的父类
可变参数
public static void main(String[] args) {
System.out.println(sum1(1, 1, 2, 3, 546, 31));
System.out.println(sum2(5,654,6545,31));
}
static int sum1(int... a) {//可变参数a实际上是一个数组
int sum = 0;
for (int i : a) {
sum += i;
}
return sum;
}
static int sum2(int a, int... b) {
int sum = 0;
for (int i : b) {
sum += i;
}
sum = sum + a;
return sum;
}
//static int sum3(int... a, int b)报错,因为可变参数必须是最后一个形参
File
构造方法
File f1 = new File("E\\java\\a.text");
System.out.println(f1);
File f2 = new File("E\\java", "b.text");
System.out.println(f2);
File f3 = new File("E\\java");
File f4 = new File(f3, "c.text");
System.out.println(f4);
文件夹创建
File f1 = new File("D:\\JAVA\\a");
System.out.println(f1.createNewFile());//创建文件,成功返回ture,失败返回false
File f2 = new File("D:\\JAVA\\b");
System.out.println(f2.mkdir());//创建文件夹,成功返回ture,失败返回false(注意这里会返回false,因为不允许出现同名,无论是文件夹还是文件)
File f3 = new File("D:\\JAVA\\A\\B");
System.out.println(f3.mkdirs());//同样的创建文件夹,但是与mkdir相比mkdirs能创建多级目录,而mkdir没有父目录就无法创建
相关方法
File f1 = new File("D:\\JAVA");
System.out.println(f1.isDirectory());//判断对象是否是路径,存在为ture,不是或不存在为false
File f2 = new File("D:\\信技\\pr\\寒招\\pr\\video.mp4");
System.out.println(f2.isFile());//判断对象是否是文件,是为ture,不是或不存在为false
System.out.println(f1.exists());
System.out.println(f2.exists());//判断对象是否存在,可以是文件也可以是文件夹
File f3 = new File("pr\\video.mp4");
//返回对象的绝对路径,无论对象是否存在(可以根据对象的路径补全)
//如例输出为 D:\JAVA\javatext\Text\pr\video.mp4
System.out.println(f3.getAbsolutePath());
//只将对象的路径转化为字符串返回
//如例输出为 pr\video.mp4
System.out.println(f3.getPath());
//返回文件或文件夹名称
//如例输出为 video.mp4
System.out.println(f3.getName());
//返回路径下所有文件或文件夹的名称
List<String> l1 = Arrays.asList(f1.list());
System.out.println(l1);
//返回路径下所有文件或文件夹的File对象
List<File> l2 = Arrays.asList(f1.listFiles());
System.out.println(l2);
File f4 = new File("D:\\JAVA\\a");
File f5 = new File("D:\\JAVA\\a\\b.txt");
System.out.println(f1.delete());//当删除的目录下有文件或文件夹时报错
System.out.println(f2.delete());
System.out.println(f1.delete());//要先删除文件夹里的文件和文件夹才能进行删除