반응형
1. 현재 날짜
public void getCurrentDate() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
LocalDate now = LocalDate.now();
this.nowYear = now.getYear();
this.nowMonth = now.getMonth().getValue();
Log.d("@@@@", "nowMonth : "+ nowMonth);
this.nowDay = now.getDayOfMonth();
} else {
Date now = new Date();
SimpleDateFormat formatterYear = new SimpleDateFormat("yyyy");
SimpleDateFormat formatterMonth = new SimpleDateFormat("MM");
SimpleDateFormat formatterDay = new SimpleDateFormat("dd");
int formatedNowYear = Integer.parseInt(formatterYear.format(now));
int formatedNowMonth = Integer.parseInt(formatterMonth.format(now));
int formatedNowDay = Integer.parseInt(formatterDay.format(now));
Log.d("@@@@", formatedNowYear+" "+formatedNowMonth+" "+formatedNowDay);
this.nowYear = formatedNowYear;
this.nowMonth = formatedNowMonth;
this.nowDay = formatedNowDay;
}
}
2. 현재시간
public void getCurrentTime() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
LocalTime now = LocalTime.now();
this.nowHour = now.getHour();
this.nowMinute = now.getMinute();
} else {
Date now = new Date();
SimpleDateFormat formatterHour = new SimpleDateFormat("HH");
SimpleDateFormat formatterMinute = new SimpleDateFormat("mm");
int formatedNowHour = Integer.parseInt(formatterHour.format(now));
int formatedNowMinute = Integer.parseInt(formatterMinute.format(now));
Log.d("@@@@", formatedNowHour + " " + formatedNowMinute);
this.nowHour = formatedNowHour;
this.nowMinute = formatedNowMinute;
}
}
3. AM, PM 시간에 따른
1) AM, PM
//메시지 예약 시간 Am Pm
public String getAmPmTen(int hour) {
String amPm;
if(hour >= 12){
amPm = getString(R.string.pm_picker);
} else{
amPm = getString(R.string.am_picker);
}
return amPm;
}
2) 24시간 -> 12시간
//메시지 예약 시간 24시간 -> 12시간 formatter
public int getFormattedHour (int hour) {
if(hour >= 13){
hour = hour - 12;
}
return hour;
}
4. 10분뒤 날짜 시간
//10분뒤 날짜 시간
public void tenMinuteLater(){
Calendar cal = Calendar.getInstance();
SimpleDateFormat formatterYear = new SimpleDateFormat("yyyy");
SimpleDateFormat formatterMonth = new SimpleDateFormat("MM");
SimpleDateFormat formatterDay = new SimpleDateFormat("dd");
SimpleDateFormat formatterHour = new SimpleDateFormat("HH");
SimpleDateFormat formatterMinute = new SimpleDateFormat("mm");
cal.add(Calendar.MINUTE, 10);
int formatedNowYear = Integer.parseInt(formatterYear.format(cal.getTime()));
int formatedNowMonth = Integer.parseInt(formatterMonth.format(cal.getTime()));
int formatedNowDay = Integer.parseInt(formatterDay.format(cal.getTime()));
int formatedNowHour = Integer.parseInt(formatterHour.format(cal.getTime()));
int formatedNowMinute = Integer.parseInt(formatterMinute.format(cal.getTime()));
this.nowYear = formatedNowYear;
this.nowMonth = formatedNowMonth;
this.nowDay = formatedNowDay;
this.nowHour = formatedNowHour;
this.nowMinute = formatedNowMinute;
}
5. 시간 날짜 DATE로 포맷
1) 날짜 DATE로 포맷
//finalTime formatter
public String getFinalTime(int mH, int mM){
finalTime = String.format("%02d", mH)+"시 "+String.format("%02d", mM)+"분 ";
Log.d(TAG + "@@@@ getFinalTime", "finalTime: " + finalTime);
return finalTime;
}
2) 시간 DATE로 포맷
//finalDate formatter
public String getFinalDate(int mY, int mM, int mD){
finalDate = String.format("%04d", mY)+"년 "+String.format("%02d", mM)+"월 "+String.format("%02d", mD)+"일 ";
Log.d(TAG + "@@@@ getFinalDate ", "finalDate: " + finalDate);
return finalDate;
}
3) 시간+날짜 DATE로 포맷
finalDateTime = finalDate + "" + finalTime;
finalDateTime을 mFinalDateTime 에 넣어준다
finalDate은 getFinalDate
finalTime은 getFinalTime
public Date stringToDate(String mFinalDateTime){
Date dateFinalDateTime = null;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy년 MM월 dd일 HH시 mm분");
try {
dateFinalDateTime = dateFormat.parse(mFinalDateTime);
Log.d(TAG + "@@@@", "dateFinalDateTime: " + dateFinalDateTime );
Log.d(TAG + "@@@@", "mFinalDateTime: " + mFinalDateTime );
} catch (Exception e){ e.printStackTrace(); }
return dateFinalDateTime;
}
Reference
https://sseong66.tistory.com/58
https://hianna.tistory.com/609
반응형
'Android > Android Java' 카테고리의 다른 글
[Android Java] 화면 회전시 새로 띄우기 (0) | 2023.10.27 |
---|---|
[Android Java] configChanges 설정 (1) | 2023.10.27 |
[Android Java] ViewModel(LiveData, Factory) (0) | 2023.10.27 |
[Android Java] CalendarPicker, TimePicker (Viewmodel, LiveData, Factory, EventBus) (2) | 2023.10.27 |
[Android Java] Room DB 사용하기 (0) | 2023.10.27 |