1.java.text包下,一般用来格式化
子类:simpleDateFormat:
格式化:
把Date类型格式化成String类型,变成任意我们想要的格式
String format(Date date)
解析:
将String类型转换成Date
Date parse(String source):从给定字符串生成一个日期文本
例,字符串“2019-3-9”——Date
SimpleDateFormat的功能测试:
构造方法:
SimpleDateFormat():无参构造,使用默认的模式进行对象的构建
SimpleDtaeFormat(String pattern):使用指定的模式进行对象的构建
例1:
public class SimpleDateFormateDemo1 {
public static void main(String[] args) throws ParseException {
//使用指定的模式进行对象的创建
//例:xxxx年xx月xx日
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
//格式化,首先要有Date对象
Date date = new Date();
//使用指定模式格式化,把日期对象换成字符串对象
String s = sdf.format(date);
System.out.println(s);
System.out.println("----------");
}
}
//解析
Date d = sdf.parse("2019年03月10日");
System.out.println(d.toLocaleString());
}*/
例2:
public class SimpleDateFormateDemo1 {
public static void main(String[] args) throws ParseException {
//在例1的基础上加上时分秒
//例2019年3月10日12:12:12
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH:mm:ss");
Date d = new Date();
//格式化
String s = sdf.format(d);
System.out.println(s);
//解析
Date dd = sdf.parse(s);
System.out.println(dd.toLocaleString());
}
}
例3:
public class SimpleDateFormateDemo1 {
public static void main(String[] args) throws ParseException {
//使用默认模式进行对象的构建
SimpleDateFormat sdf = new SimpleDateFormat();
//创建日期对象
Date d = new Date();
//使用默认模式格式化,把日期对象转换成字符串对象
String s = sdf.format(d);
System.out.println(s); //19-3-10 下午12:25系统的当前时间
System.out.println("----------");
//解析,把字符串对象转换成日期
Date sb = sdf.parse("19-3-10 下午12:25");
System.out.println(sb);
System.out.println(sb.toLocaleString());
}*/
}
网友评论