본문 바로가기

Java

JAVA 오늘 날짜 어제 내일 작년 등 구하기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 오늘 날짜
Calendar cal = Calendar.getInstance();
String format = "yyyy-MM-dd";
SimpleDateFormat sdf = new SimpleDateFormat(format);
String date = sdf.format(cal.getTime());
System.out.println(date);
 
 
 
// 어제 날짜
Calendar cal = Calendar.getInstance();
String format = "yyyy-MM-dd";
SimpleDateFormat sdf = new SimpleDateFormat(format);
cal.add(cal.DATE, -1); //날짜를 하루 뺀다.
String date = sdf.format(cal.getTime());
System.out.println(date);
 

 

 

 

 

1
2
3
4
5
6
Calendar cal = Calendar.getInstance();
String format = "yyyy-MM-dd";
SimpleDateFormat sdf = new SimpleDateFormat(format);
cal.add(cal.MONTH, -3); //세달 전
String date = sdf.format(cal.getTime());
System.out.println(date);
cs

 

 

 

 

 

 

 

https://myhappyman.tistory.com/45

 

Java - 오늘, 어제, 이번달, 올해, 지난해 등 날짜 구하기

Java 1.8버전 이하에서는 날짜를 구하려면 Calendar 객체를 호출하고 데이트포맷을 설정하여 문자열로 받아와 출력을 해줘야 한다. Java에서 날짜를 구하는 함수를 만들어서 공유하고자 포스팅한다. 오늘 날짜 구..

myhappyman.tistory.com