오늘 날짜, 오늘 지금으로부터 24시간(하루전) 날짜 구하기

JooKit 주킷 2022. 6. 10. 16:02
목차 접기
728x90
반응형

 

지금 날짜(오늘 날짜)

Calendar now = Calendar.getInstance(); 
int year = now.get(Calendar.YEAR); 
int month = now.get(Calendar.MONTH) +1; 
int day = now.get(Calendar.DAY_OF_MONTH); 
int week = now.get(Calendar.DAY_OF_WEEK); 
int amPm = now.get(Calendar.AM_PM); 
int hour = now.get(Calendar.HOUR_OF_DAY); 
int minute = now.get(Calendar.MINUTE); 
int second = now.get(Calendar.SECOND);

System.out.println("year : " + year);
System.out.println("month : " + month);
System.out.println("day : " + day);
System.out.println("week : " + week);
System.out.println("amPm : " + amPm);
System.out.println("hour : " + hour);
System.out.println("minute : " + minute);
System.out.println("second : " + second);

 

 

지금으로부터 24시간 전 날짜 

  Calendar day2 = Calendar.getInstance();    
  day2.add(Calendar.DATE , -1);    
  int year2 = day2.get(Calendar.YEAR);
  int month2 = day2.get(Calendar.MONTH) + 1;
  int day22 = day2.get(Calendar.DAY_OF_MONTH);
  int hour2 = day2.get(Calendar.HOUR_OF_DAY);
  int minute2 = day2.get(Calendar.MINUTE);
  int second2 = day2.get(Calendar.SECOND);

  System.out.println("year2 : " + year2);
  System.out.println("month2 : " + month2);
  System.out.println("day22 : " + day22);
  System.out.println("hour2 : " + hour2);
  System.out.println("minute2 : " + minute2);
  System.out.println("second2 : " + second2);

 

지금으로부터 24시간 전 날짜(시간까지 한번에 표현)

  Calendar day2 = Calendar.getInstance();    
  day2.add(Calendar.DATE , -1);    
  String beforeDate = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(day2.getTime());    
  System.out.println(beforeDate);

 

 

합쳐서 사용

 

728x90
반응형
LIST

'Java' 카테고리의 다른 글

이클립스에서 local history 찾는 방법  (0) 2022.06.22
[ Java ] AES-256으로 암호화 복호화 방법  (0) 2022.06.10
[ sts ] select type, matching item 해결  (1) 2022.06.03
InetAddress 설명  (0) 2022.03.07
[ java ] client ip 얻기  (0) 2022.03.07