package com.hta.lecture.dto;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@NoArgsConstructor
@Setter
@Getter
public class UserCouponDto {
private int userCouponNo;
private int userNo;
private int couponNo;
private int usePeriod;
private String couponName;
private int discountRate;
private int discountPrice;
private String useStatus;
private Date useDate;
private String periodStatus;
@JsonFormat(pattern = "yyyy년 M월 d일")
private Date pubDate;
@JsonFormat(pattern = "yyyy년 M월 d일")
private Date periodDate;
// 남은 유효기간.
public long getRemainDate() {
Date period = this.getPeriodDate(); // 유효기간
Date now = new Date(); // 현재날짜
long remainDate; // 남은기간
if(period.getTime() < now.getTime()) {
remainDate = 0;
}
long diffSec = (period.getTime() - now.getTime()) / 1000;
remainDate = diffSec/(24*60*60);
return remainDate;
}
// remainDate 메서드 실행.
public static void main(String[] args) throws ParseException {
UserCouponDto coupon = new UserCouponDto();
String period = "2022/02/25";
try {
Date periodDate = new SimpleDateFormat("yyyy/MM/dd").parse(period);
coupon.setPeriodDate(periodDate);
} catch (ParseException e) {
e.printStackTrace();
}
long remainDate = coupon.getRemainDate();
System.out.println(remainDate);
}
}
getRemainDate 메서드를 실행해 d-day의 날짜를 출력한다.