1.获取当前时间的前一个月
Date current = new Date();
Calendar cldCurrent = Calendar.getInstance(java.util.TimeZone.getTimeZone("GMT+8:00"));
cldCurrent.setTime(current);
cldCurrent.add(Calendar.MONTH, -Integer.parseInt(monthnum));
Date LastDay = cldCurrent.getTime();
2.将8位或12位或14位的数值型日期时间转换为10位或16位或19带格式的日期时间。 型如: 20040101 转换为 2004/01/01,或 200401010101 转换为 2004/01/01 01:01,或 20040101010101 转换为 2004/01/01 01:01:01。
输入参数:Long date 十二位或十四位的数字日期时间。 返回参数:String fmDate 16位或19位带格式的日期
public static String getStrFmDate(Long date) {
String fmDate = new String();
if (date == null) {
fmDate = "";
}
else {
fmDate = date.toString();
String year = "";
String month = "";
String day = "";
String hour = "";
String minute = "";
//日期格式不合法则转化为空串
if (fmDate.length() < 8) {
fmDate = "";
}
if (fmDate.length() >= 8) {
year = fmDate.substring(0, 4);
month = fmDate.substring(4, 6);
day = fmDate.substring(6, 8);
fmDate = year + "/" + month + "/" + day;
}
if ( (date.toString()).length() >= 12) {
hour = (date.toString()).substring(8, 10);
minute = (date.toString()).substring(10, 12);
fmDate = fmDate + " " + hour + ":" + minute;
}
if ( (date.toString()).length() == 14) {
fmDate = fmDate + ":" + (date.toString()).substring(12, 14);
}
}
return fmDate;
}
3.获取当前系统时间
/**
* 功能:得到系统当前时间显示,格式:yyyy/mm/dd hh:mm主要是为页面显示用
* 输入参数:
* 返回参数:String:系统当前时间
*/
public static String getCurrentDateTimeView() {
//获得当前日期
Calendar cldCurrent = Calendar.getInstance(java.util.TimeZone.getTimeZone(
"GMT+8:00"));
//获得年月日
String strYear = String.valueOf(cldCurrent.get(Calendar.YEAR));
String strMonth = String.valueOf(cldCurrent.get(Calendar.MONTH) + 1);
String strDate = String.valueOf(cldCurrent.get(Calendar.DATE));
String srtHours = String.valueOf(cldCurrent.get(Calendar.HOUR_OF_DAY));
String strMinute = String.valueOf(cldCurrent.get(Calendar.MINUTE));
//整理格式
if (strMonth.length() < 2) {
strMonth = "0" + strMonth;
}
if (strDate.length() < 2) {
strDate = "0" + strDate;
}
if (srtHours.length() < 2) {
srtHours = "0" + srtHours;
}
if (strMinute.length() < 2) {
strMinute = "0" + strMinute;
}
//得出当天日期时间的字符串
String StrCurrentCalendar = strYear + "/" + strMonth + "/" + strDate +
" "
+ srtHours + ":" + strMinute;
return StrCurrentCalendar;
}