1、时间戳用法:
方法 一 :System.currentTimeMillis();
方法 二 :Calendar.getInstance().getTimeInMillis();
方法 三 :
new Date().getTime();
Timestamp stamp1=new Timestamp(SUtil.parseDate(sdate).getTime()); sdate为String类型
system.out.println(stamp1.getTime());
2、将日期格式转换为"yyyy-MM-dd"的日期格式
Data date=new java.sql.Date(new Date().getTime());
3、jsp页面中日期格式的转换
String DATETIME_FORMAT="yyyy-MM-dd HH:mm:SS";
String DATETIME_MIN_FORMAT="yyyy-MM-dd HH:mm";
String DATE_FORMAT="yyyy-MM-dd";
String TIME_FORMAT="HH:mm:ss";
<fmt:formatDate value="${assertUseRequest.requestTime}" pattern="yyyy年MM月dd日 HH时" />
SimpleDateFormat fymd = new SimpleDateFormat ("yyyy年MM月dd日 HH时");
fymd.format(assertUseRequest.getRequestTime())
4、按格式要求,获得当前时间,按字符串格式输出
public static String getNow(String pattern) {
return DateFormatUtils.format(new Date(), pattern);
}
5、按格式要求,获得指定的日期,按字符串格式输出
public static String datef(Date date,String pattern) {
if(date!=null){
return DateFormatUtils.format(date, pattern);
}else{
return null;
}
}
6、将日期转换为小时数
public static Double hour(Date time){
SimpleDateFormat st=new SimpleDateFormat("yyyyMMddHH");
return Double.parseDouble(st.format(time));
}
7、获得指定日期是星期几.
public static Week weekOfDate(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int x = calendar.get(Calendar.DAY_OF_WEEK)-1;
return Week.values()[x];
}
8、从日期的字符串形式,转换成Date型,如果转换失败,则转换为当前时间
/**日期格式*/
String DATETIME_FORMAT="yyyy-MM-dd HH:mm:ss";
String DATETIME_MIN_FORMAT="yyyy-MM-dd HH:mm";
String DATE_FORMAT="yyyy-MM-dd";
String TIME_FORMAT="HH:mm:ss";
String[] FORMAT_PATTERNS= {DATETIME_FORMAT,DATETIME_MIN_FORMAT,DATE_FORMAT,TIME_FORMAT};
public static Date parseDate(String datestr) {
try {
return DateUtils.parseDate(datestr, FORMAT_PATTERNS);
} catch (ParseException e) {
}
return new Date();
}
9、判断当前日期是星期几
public static int dayForWeek(String pTime) throws Exception {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd" );
Calendar c = Calendar.getInstance();
c.setTime(format.parse(pTime));
int dayForWeek = 0 ;
if (c.get(Calendar.DAY_OF_WEEK) != 1 ){
dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1 ;
}
return dayForWeek;
}
10、获得该日期所在星期的所有日期
public static String[] startEndOfWeek(String date,Integer week){
String[] weeks = new String[7];
Calendar StartCalendar = Calendar.getInstance();
StartCalendar.setTime(parseDate(date));
StartCalendar.add(Calendar.DAY_OF_MONTH,-week);
weeks[0]=datef(StartCalendar.getTime(), DATE_FORMAT);
for(int i=1;i<7;i++){
StartCalendar.add(Calendar.DAY_OF_MONTH,+1);
weeks[i]=datef(StartCalendar.getTime(), DATE_FORMAT);
}
return weeks;
}