//方法一:内部引用
package com.th.mobula.test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormat {
private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
public static void main(String[] args) {
try {
Date date = simpleDateFormat.parse("2019-10-31");
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
//-----------------------------------------------------------------------------------------
//方法二:外部引用
package com.th.mobula.test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormat {
public static void main(String[] args) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = simpleDateFormat.parse("2019-01-02");
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("the outline date is "+ date);
}
}
/*
* Date date = null; 这句不可省略,如果省略try catch范围外将不可引用date变量,即
* System.out.println("the outline date is "+ date); 这句中的date将无法引用变量值
*/