현재 날짜
시스템 currentTimeMillis로 현재 날짜와 시간을 구하고 Date로 만듬
val date = Date(System.currentTimeMillis())
DateFormat으로 년도 월 일 시 분 초로 원하는 형식(중간에 기호 등)으로 출력 가능 ("yyyy/MM/dd HH:mm:ss")
시간을 표시할 때 HH 면 24시 기준 hh면 12 기준이다
val sdfCurrent = SimpleDateFormat("yyyyMMddHHmm")
String으로 변환
val formatDate: String = sdfCurrent.format(date)
사용 취지에 맞게 long이나 int로 변환하여 사용 (년도에서 분까지 쓰게되면 long 추천 - 자료값 범위 때문
val currentDate = formatDate.toLong()
//요약 가능
val currentDate = SimpleDateFormat("yyyyMMddHHmm").format(Date(System.currentTimeMillis())).toLong()
과거 날짜
//오늘
Date today = new Date();
SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
String toDay = date.format(today);
System.out.println(toDay);
// 1시간전
Calendar cal = Calendar.getInstance();
cal.setTime(today);
cal.add(Calendar.HOUR, -1);
// 포맷변경 ( 년월일 시분초)
SimpleDateFormat sdformat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
sdformat.setTimeZone(TimeZone.getTimeZone("UTC"));
String beforeHour = sdformat.format(cal.getTime());
System.out.println("1시간 전 : " + beforeHour);
//하루 전
Calendar day = Calendar.getInstance();
day.add(Calendar.DATE , -1);
String beforeDate = new java.text.SimpleDateFormat("yyyy-MM-dd").format(day.getTime());
System.out.println(beforeDate);
//한주 전
Calendar week = Calendar.getInstance();
week.add(Calendar.DATE , -7);
String beforeWeek = new java.text.SimpleDateFormat("yyyy-MM-dd").format(week.getTime());
System.out.println(beforeWeek);
//한달 전
Calendar mon = Calendar.getInstance();
mon.add(Calendar.MONTH , -1);
String beforeMonth = new java.text.SimpleDateFormat("yyyy-MM-dd").format(mon.getTime());
System.out.println(beforeMonth);
'Android > Reference' 카테고리의 다른 글
Indicator (인디케이터) 실습 (0) | 2021.03.29 |
---|---|
RecyclerView GridLayoutManager spansize 커스텀 (0) | 2021.03.26 |
Floating Action Button (플로팅 액션 버튼) (2) | 2021.01.04 |
RecyclerView 화면깜빡임 현상 방지 (ItemAnimator) (0) | 2020.12.16 |
RecyclerView 엣지 스크롤 이펙트 숨기기 (0) | 2020.12.16 |