import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;import java.net.URLEncoder;
import java.sql.Timestamp;
public class CurrentDateTime {
private SimpleDateFormat dFormat;
private Date date;
/**
* 获取当前日期时间,自定义格式
*
* @param String
* @return datetime
* @日期 【G ※ 年代】,【y ※ 年】,【M ※ 月】,【d ※ 日】
* @时间 【a ※ 上午 / 下午】,【h ※ (1~12)时】,【H ※ (0~23)时】,【m ※ 分】,【s ※ 秒】,【S ※ 毫秒】
* @星期 【E ※ 星期】,【F ※ 一月中第几个星期几】,【w ※ 一年中第几个星期】,【W ※ 一月中第几个星期】
* @其他 【D ※ 一年中的第几天】,【z ※ 时区】,【k ※ (1~24)时】,【K ※ (0~11)时】
*/
public String getDateTime(String format) {
date = new Date(new Date().getTime());
dFormat = new SimpleDateFormat(format);
String datetime = dFormat.format(date);
return datetime;
}
/**
* 获取当前日期
*
* @return date
*/
public String getDate() {
dFormat = new SimpleDateFormat("yyyy-MM-dd");
date = new Date(new Date().getTime());
String datestr = dFormat.format(date);
return datestr;
}
/**
* 获取当前日期时间
*
* @return Timestamp
*/
public String getTimestamp() {
Timestamp time = new Timestamp(System.currentTimeMillis());
return time.toString();
}
/**
* 获取当前日期时间
*
* @return UrlTimestamp
*/
public String getUrlTimestamp() {
Timestamp time = new Timestamp(System.currentTimeMillis());
return URLEncoder.encode(time.toString());
}
/**
* 获取某年某月的天数
*
* @param year
* @param month
* @return
*/
public Integer getLastDayOfMonth(String year, String month) {
Integer Year, Month;
if (year != null && month != null) {
Year = Integer.valueOf(year);
Month = Integer.valueOf(month);
Calendar c = Calendar.getInstance();
c.set(Year, Month, 1);
c.add(c.DAY_OF_YEAR, -1);
return c.get(c.DAY_OF_MONTH);
} else {
System.out.println("年月不能为空:" + year + "年" + month + "月");
return 0;
}
}
public static void main(String[] args) {
CurrentDateTime current = new CurrentDateTime();
System.out.println(current.getDateTime("yyyy"));
System.out.println(current.getDateTime("ww"));
}
}
实例化,想得到什么样的格式,随便你!!!