获取时间的工具类
public static String getDateBefore() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -10);
Date date = calendar.getTime();
DateFormat df = new SimpleDateFormat("yyyyMMdd");
String format = df.format(date);
return format;
}
getFirstDay();
public static String getFirstDay() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
Date theDate = calendar.getTime();
GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
gcLast.setTime(theDate);
gcLast.set(Calendar.DAY_OF_MONTH, 1);
String day_first = df.format(gcLast.getTime());
StringBuffer str = new StringBuffer().append(day_first).append("");
return str.toString();
}
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
System.out.println(simpleDateFormat.format(getCurrYearFirst())
public static Date getCurrYearFirst(){
Calendar currCal=Calendar.getInstance();
int currentYear = currCal.get(Calendar.YEAR);
return getYearFirst(currentYear);
}
public static Date getYearFirst(int year){
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.YEAR, year);
Date currYearFirst = calendar.getTime();
return currYearFirst;
}
public static void main(String[] args) throws ParseException {
System.out.println(System.nanoTime());
long sd = System.currentTimeMillis();
System.out.println(sd);
SimpleDateFormat ss = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String format1 = ss.format(new Date());
Date date = ss.parse(format1);
long longDate = date.getTime();
System.out.println("指定时间"+longDate);
Date dat=new Date(longDate);
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(dat);
java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String sb=format.format(gc.getTime());
System.out.println("转换指定时间"+sb);
Date lastHourTime = getLastHourTime(new Date(), 1);
String format2 = ss.format(lastHourTime);
System.out.println(format2);
}
public static Date getCurrentHourTime() {
return getHourTime(new Date(), 0, "=");
}
public static Date getLastHourTime(Date date, int n) {
return getHourTime(date, n, "-");
}
public static Date getNextHourTime(Date date, int n) {
return getHourTime(date, n, "+");
}
public static Date getHourTime(Date date, int n, String direction) {
Calendar ca = Calendar.getInstance();
ca.setTime(date);
ca.set(Calendar.MINUTE, 0);
ca.set(Calendar.SECOND, 0);
switch (direction) {
case "+":
ca.set(Calendar.HOUR_OF_DAY, ca.get(Calendar.HOUR_OF_DAY) + n);
break;
case "-":
ca.set(Calendar.HOUR_OF_DAY, ca.get(Calendar.HOUR_OF_DAY) - n);
break;
case "=":
ca.set(Calendar.HOUR_OF_DAY, ca.get(Calendar.HOUR_OF_DAY));
break;
default:
ca.set(Calendar.HOUR_OF_DAY, ca.get(Calendar.HOUR_OF_DAY));
}
date = ca.getTime();
return date;
}
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.DATE, + 7);
Date d = c.getTime();
String day = format.format(d);
System.out.println("七天后:"+day);
c.setTime(new Date());
c.add(Calendar.MONTH, +1);
Date m = c.getTime();
String mon = format.format(m);
System.out.println("个月后:"+mon);
c.setTime(new Date());
c.add(Calendar.YEAR, +1);
Date y = c.getTime();
String year = format.format(y);
System.out.println("一年后:"+year);