Back/Spring Java

특정일로 부터 날짜 계산 (Java)

밍꿔 2020. 7. 28. 17:28


반응형

 

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;
    }
}
반응형