java获取系统指定时间年月日

时间:2021-10-18 22:30:45

java获取系统指定时间年月日

     private String setDateTime(String falg) {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(new Date().getTime());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat dateMouthFormat = new SimpleDateFormat("yyyy-MM");
SimpleDateFormat dateYearFormat = new SimpleDateFormat("yyyy");
if (falg.equals("daystartTime")) {
c.add(Calendar.DATE, -45);
return dateFormat.format(c.getTime());
} else if (falg.equals("dayendTime")) {
return dateFormat.format(c.getTime());
} else if (falg.equals("MouthstartTime")) {
c.add(Calendar.YEAR, -1);
return dateMouthFormat.format(c.getTime());
} else if (falg.equals("MouthendTime")) {
return dateMouthFormat.format(c.getTime());
} else if (falg.equals("YearstartTime")) {
c.add(Calendar.YEAR, -3);
return dateYearFormat.format(c.getTime());
} else if (falg.equals("YearendTime")) {
return dateYearFormat.format(c.getTime());
} else {
return null;
}
}