时间的格式详情: y 年 M 月 d 日 h 时 在上午或下午 (1~12) H 时 在一天中 (0~23) m 分 s 秒 S 毫秒 E 星期
D 一年中的第几天 F 一月中第几个星期几 w 一年中第几个星期 W 一月中第几个星期
SimpleDateFormat继承关系:
java.lang.Object
|
+----java.text.Format
|
+----java.text.DateFormat
|
+----java.text.SimpleDateFormat
设置需要输出的时间类型:写法可以不同 format()
Date date = new Date();
SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
SimpleDateFormat myFmt1=new SimpleDateFormat("yy/MM/dd HH:mm:ss");
SimpleDateFormat myFmt2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 等同于 Date.toLocaleString()方法;
SimpleDateFormat myFmt4=new SimpleDateFormat("一年中的第 D 天 一年中第w个星期 一月中第W个星期 在一天中k时 z时区");
时间戳转换为时间类型:
public static String getDate(String s){
SimpleDateFormat x = new SimpleDateFormat("yyyy-mm-dd");
long a = new Long(s);
Date date=new Date(a);
String time = x.format(date);
System.out.println(time);
return time;
}
时间转换为时间戳类型:
public static String getDate2(String date) throws ParseException{
SimpleDateFormat time =new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
Date d =time.parse(date);
String times =String.valueOf(d.getTime());
return times;
}
获取传入年,月的最后一天:
public static String getlastdayofmonth(int year,int month){
Calendar cal = Calendar.getInstance();
//设置年份
cal.set(Calendar.YEAR,year);
//设置月份
cal.set(Calendar.MONTH, month-1);
//获取某月最大天数
int lastDay=cal.getActualMaximum(Calendar.DAY_OF_MONTH);
System.out.println(lastDay);
//设置日历中月份的最大天数
cal.set(Calendar.DAY_OF_MONTH, lastDay);
//格式化日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String lastDayOfMonth = sdf.format(cal.getTime());
return lastDayOfMonth;
}