可以获取当前时间前5min 10min 30min 60min 1day的时间
方便开发时使用到时间段的时候调用
package com.struct;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Str_Time {
Calendar ca=Calendar.getInstance();
String year = ca.get(Calendar.YEAR) + "";
String month = (ca.get(Calendar.MONTH) + 1) < 10 ? "0"
+ (ca.get(Calendar.MONTH) + 1) : ca.get(Calendar.MONTH) + 1 + "";
String date = (ca.get(Calendar.DATE)) < 10 ? "0" + ca.get(Calendar.DATE)
: ca.get(Calendar.DATE) + "";
String hour = (ca.get(Calendar.HOUR_OF_DAY) < 10 ? "0"
+ ca.get(Calendar.HOUR_OF_DAY) : ca.get(Calendar.HOUR_OF_DAY) + "");
String minute = (ca.get(Calendar.MINUTE) < 10 ? "0"
+ ca.get(Calendar.MINUTE) : ca.get(Calendar.MINUTE) + "");
String second = (ca.get(Calendar.SECOND) < 10 ? "0"
+ ca.get(Calendar.SECOND) : ca.get(Calendar.SECOND) + "");
//获取当前年月日,小时分钟秒,并按照需求格式化为 2016-07-08 12:01:01这种格式
private SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public String getFiveMinTime() {
Calendar ca=Calendar.getInstance();
int now = ca.get(Calendar.MINUTE);
ca.set(Calendar.MINUTE, now - 5);
String date = SDF.format(ca.getTime());
return date;
}//获取当前时间前五分钟
public String getTenMinTime(){
Calendar ca=Calendar.getInstance();
int now = ca.get(Calendar.MINUTE);
ca.set(Calendar.MINUTE, now - 10);
String date = SDF.format(ca.getTime());
return date;
}//获取当前时间前十分钟
public String get30MinTime(){
Calendar ca=Calendar.getInstance();
int now = ca.get(Calendar.MINUTE);
ca.set(Calendar.MINUTE, now - 30);
String date = SDF.format(ca.getTime());
return date;
}//获取当前时间前半小时
public String get60MinTime(){
Calendar ca=Calendar.getInstance();
int now = ca.get(Calendar.MINUTE);
ca.set(Calendar.MINUTE, now - 60);
String date = SDF.format(ca.getTime());
return date;
}//获取当前时间前一小时
public String getOneDayTime(){
Calendar ca=Calendar.getInstance();
int now = ca.get(Calendar.DATE);
ca.set(Calendar.DATE, now - 1);
String date = SDF.format(ca.getTime());
return date;
}//获取当前时间前一天
public String getNowTime() {
Calendar ca=Calendar.getInstance();
String date = SDF.format(ca.getTime());
return date;
}//获取当前时间
public String getYear() {
return year;
}
public String getMonth() {
return month;
}
public String getDate() {
return date;
}
public String getHour() {
return hour;
}
public String getMinute() {
return minute;
}
public String getSecond() {
return second;
}
}
网友评论