沁阳网站建设,h5商城,wordpress炫酷背景,实验一 电子商务网站建设与维护DateFormat类的作用#xff1a; 把时间对象转化成指定格式的字符串。反之#xff0c;把指定格式的字符串转化成时间对象。DateFormat是一个抽象类#xff0c;一般使用它的子类SimpleFateFormat类来实现。
DateFormat类和SimpleDateFormat类的使用#xff1a;
import java…DateFormat类的作用 把时间对象转化成指定格式的字符串。反之把指定格式的字符串转化成时间对象。DateFormat是一个抽象类一般使用它的子类SimpleFateFormat类来实现。
DateFormat类和SimpleDateFormat类的使用
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;public class TestDateFormat {public static void main(String[] args){//new 出 SimpleDateFormat 对象SimpleDateFormat s1 new SimpleDateFormat(yyyy-MM-dd hh:mm:ss);SimpleDateFormat s2 new SimpleDateFormat(yyyy-MM-dd);//将时间对象转换成字符串String daytime s1.format(new Date());System.out.println(daytime);System.out.println(s2.format(new Date()));System.out.println(new SimpleDateFormat(hh:mm:ss).format(new Date()));//将符合指定格式的字符串转成时间对象字符串格式需要和指定格式一致String time 2049-10-1;Date date s2.parse(time);System.out.println(date1: date);time 2049-10-1 20:15:30;date s1.parse(time);System.out.println(date2: date);}
}
代码中的格式化字符的具体含义见表
字母日期或时间元素表示示例GEra标识符TextADy年Year199696M年中的月份MonthJujlyJul07w年中的周数Number27W月份中的周数Number2D年中的天数Number189d月份中的天数Number10F月份中的星期Number2E星期中的天数TextTuesdayTueaAm/pm标记TextPMH一天中的小时数0-23Number0k一天中的小时数1-24Number24Kam/pm中的小时数0-11Number0ham/pm中的小时数1-12Number12m小时中的分钟数Number30s分钟中的秒数Number55S毫秒数Number978z时区General time zonePacific Standard TimePSTGMT-08:00Z时区RFC 822 time zone08:00
时间格式字符也可以为我们提供其他的便利。比如获得当前时间是今年的第几天。
获取今天是本年度第几天
import java.text.SimpleDateFormat;
import java.util.Date;publlic class TestDateFormat2 {SimpleDateFormat s1 new SimpleDateFormat(D);String daytime s1.format(new Date());System.out.println(daytime);
}