반응형
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class DateUtils {
// 현재 날짜
public static String getToday(){
DecimalFormat df = new DecimalFormat("00");
Calendar currentCalendar = Calendar.getInstance();
String strYear = Integer.toString(currentCalendar.get(Calendar.YEAR));
String strMonth = df.format(currentCalendar.get(Calendar.MONTH) + 1);
String strDay = df.format(currentCalendar.get(Calendar.DATE));
String strDate = strYear + "-" + strMonth + "-" + strDay;
return strDate;
}
// 한달 전
public static String getMonthAgo() {
DecimalFormat df = new DecimalFormat("00");
Calendar currentCalendar = Calendar.getInstance();
currentCalendar.add(currentCalendar.MONTH, -1);
String strYear31 = Integer.toString(currentCalendar.get(Calendar.YEAR));
String strMonth31 = df.format(currentCalendar.get(Calendar.MONTH) + 1);
String strDay31 = df.format(currentCalendar.get(Calendar.DATE));
String strDate31 = strYear31 + "-" + strMonth31 + "-" + strDay31;
return strDate31;
}
// 일주일 전
public static String getWeekAgo() {
DecimalFormat df = new DecimalFormat("00");
Calendar currentCalendar = Calendar.getInstance();
currentCalendar.add(currentCalendar.DATE, -7);
String strYear7 = Integer.toString(currentCalendar.get(Calendar.YEAR));
String strMonth7 = df.format(currentCalendar.get(Calendar.MONTH) + 1);
String strDay7 = df.format(currentCalendar.get(Calendar.DATE));
String strDate7 = strYear7 + "-" + strMonth7 + "-" + strDay7;
return strDate7;
}
// 하루 전
public static String getDayAgo() {
DecimalFormat df = new DecimalFormat("00");
Calendar currentCalendar = Calendar.getInstance();
currentCalendar.add(currentCalendar.DATE, -1);
String strYear7 = Integer.toString(currentCalendar.get(Calendar.YEAR));
String strMonth7 = df.format(currentCalendar.get(Calendar.MONTH) + 1);
String strDay7 = df.format(currentCalendar.get(Calendar.DATE));
String strDate7 = strYear7 + "-" + strMonth7 + "-" + strDay7;
return strDate7;
}
// 일 계산
public static String getCalculateDay(String date, int day) {
DecimalFormat df = new DecimalFormat("00");
String[] dateArr = date.split("-");
Calendar currentCalendar = new GregorianCalendar(Integer.parseInt(dateArr[0]), Integer.parseInt(dateArr[1]), Integer.parseInt(dateArr[2]));
currentCalendar.add(currentCalendar.DATE, day);
String strYear7 = Integer.toString(currentCalendar.get(Calendar.YEAR));
String strMonth7 = df.format(currentCalendar.get(Calendar.MONTH));
String strDay7 = df.format(currentCalendar.get(Calendar.DATE));
String strDate7 = strYear7 + "-" + strMonth7 + "-" + strDay7;
return strDate7;
}
// 월 계산
public static String getCalculateMonth(String date, int month) {
DecimalFormat df = new DecimalFormat("00");
String[] dateArr = date.split("-");
Calendar currentCalendar = new GregorianCalendar(Integer.parseInt(dateArr[0]), Integer.parseInt(dateArr[1]), Integer.parseInt(dateArr[2]));
currentCalendar.add(currentCalendar.MONTH, month);
String strYear7 = Integer.toString(currentCalendar.get(Calendar.YEAR));
String strMonth7 = df.format(currentCalendar.get(Calendar.MONTH));
String strDay7 = df.format(currentCalendar.get(Calendar.DATE));
String strDate7 = strYear7 + "-" + strMonth7 + "-" + strDay7;
return strDate7;
}
}
반응형
'Back > Spring Java' 카테고리의 다른 글
Spring class 동적 로딩 (Singleton) (1) | 2021.04.05 |
---|---|
PreparedStatement 대량 insert(addBatch, executeBatch) (0) | 2021.03.10 |
압축파일 생성 (0) | 2020.07.03 |
Spring 필드주입(@Autowired)보다 생성자주입을 권장하는 이유 (0) | 2020.05.25 |
@Async 비동기 호출 (0) | 2020.04.27 |