package com.yuan.temp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtils {
private static String format = "yyyy-MM-dd HH:mm:ss";
private DateUtils() {
}
/**
* 将字符串转化为指定格式的日期
*
* @param date 时间
* @param format 格式
* @return
* @throws ParseException
*/
public static Date stringToDate(String date, String format) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.parse(date);
}
public static Date stringToDate(String date) throws ParseException {
return stringToDate(date, format);
}
/**
* 将日期转化为指定格式的字符串
*
* @param date 时间
* @param format 格式
* @return
*/
public static String dategToString(Date date, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(date);
}
public static String dategToString(Date date) {
return dategToString(date, format);
}
/**
* 演示日历类获取日期
*/
public static void calendarDemo1() {
Calendar calendar = Calendar.getInstance();
int yeay = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DATE);
System.out.println("当前日期 = " + yeay + "年" + month + "月" + day + "日");
}
/**
* 演示日历类获取日期
*/
public static void calendarDemo2() {
Calendar calendar = Calendar.getInstance();
//去年的今天
calendar.add(Calendar.YEAR, -1);
int yeay = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DATE);
System.out.println("当前日期 = " + yeay + "年" + month + "月" + day + "日");
}
/**
* 演示日历类获取日期
*/
public static void calendarDemo3() {
Calendar calendar = Calendar.getInstance();
//设置当前日历2008/8/8
calendar.set(2008, 8 - 1, 8);
int yeay = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DATE);
System.out.println("当前日期 = " + yeay + "年" + month + "月" + day + "日");
}
}
【System.out.println(new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(new Date(fb.lastModified())));】