본문 바로가기
안드로이드 자바

[JAVA][Android] 현재 시간, 날짜, 밀리초 등등 가져오기

by teamnova 2021. 9. 28.

안녕하세요 오늘은 java에서 현재 시간을 밀리초 단위로 가져오고 시간만 받아오거나 년도 월 등등 다양한 방식으로 사용하는 것에 대해 알아보아요~~ 

 

안드로이드를 사용하다 보면 현재 시간을 받아와야 하는 경우가 많습니다. 이때 하나하나 코딩을 하기에는 너무 시간이 많이 들기에 하나의 Class로 만들어 놓고 인스턴스화 시켜 원하는 데이터를 메소드호출을 통하여 받아오는게 매우 편합니다. 이에 제가 만든 Class에 대해 설명드리고 여러분도 편하게 사용 가능 하 도록 공유 하겠습니다.

 

 

 

먼저 java에서는 simpleDateFromat 이라는 메소드를 통하여 특정 형식으로 날짜를 받아 올 수 있습니다.

 

그리고 Date클래스를 사용하여 현재 날짜를 받아 올 수 있습니다.

 

이 두가지 조합을 통하여 내가 원하는 형태로 날짜를 받아 올 수 있습니다.

 

 

 

 

 

제가 사용하는 미리 만들어 놓은 Class를 첨부해 둘 테니 편하게 사용 하시기 바랍니다.

아래의 제 스틱코드를 구독하시면 됩니다.

 

https://stickode.com/profile.html?no=1356&tab=post 

 

https://stickode.com/profile.html?no=1356&tab=post

 

stickode.com

 

public class DateTime {

private int year;
private int month;
private int day;
private int time;
private int minute;
private int second;

private String yearString;
private String monthString;
private String dayString;

public DateTime(){
Calendar c = Calendar.getInstance();

setYear(c.get(Calendar.YEAR));
setMonth(c.get(Calendar.MONTH));
setDay(c.get(Calendar.DAY_OF_MONTH));
}

public String getYearString() {
return yearString;
}

public void setYearString(String yearString) {
this.yearString = yearString;
}

public String getMonthString() {
return monthString;
}

public void setMonthString(String monthString) {
this.monthString = monthString;
}

public String getDayString() {
return dayString;
}

public void setDayString(String dayString) {
this.dayString = dayString;
}



public int getYear() {
return year;
}

public void setYear(int year) {
this.year = year;
}

public int getMonth() {
return month;
}

public void setMonth(int month) {
this.month = month;
}

public int getDay() {
return day;
}

public void setDay(int day) {
this.day = day;
}

public int getTime() {
return time;
}

public void setTime(int time) {
this.time = time;
}

public int getMinute() {
return minute;
}

public void setMinute(int minute) {
this.minute = minute;
}

public int getSecond() {
return second;
}

public void setSecond(int second) {
this.second = second;
}


public Integer getNowHour(){
long now = System.currentTimeMillis();
Date mDate = new Date(now);

SimpleDateFormat sdfNow = new SimpleDateFormat("HH");
String Time = sdfNow.format(mDate);

int nowTime =Integer.parseInt(Time);
return nowTime;
}


public String getNowTime(){
long now = System.currentTimeMillis();
Date mDate = new Date(now);

SimpleDateFormat sdfNow = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String Time = sdfNow.format(mDate);

String nowTime =Time;
return nowTime;

}

public String getNowYearMonth(){
long now = System.currentTimeMillis();
Date mDate = new Date(now);
SimpleDateFormat simpleDateFormatYear = new SimpleDateFormat("yyyy");
SimpleDateFormat simpleDateFormatMonth = new SimpleDateFormat("MM");
String year = simpleDateFormatYear.format(mDate);
String month = simpleDateFormatMonth.format(mDate);
String yearMonth = year + "." + month;

return yearMonth;
}

public String getNowYearMonthDay(){
long now = System.currentTimeMillis();
Date mDate = new Date(now);
SimpleDateFormat simpleDateFormatYear = new SimpleDateFormat("yyyy");
SimpleDateFormat simpleDateFormatMonth = new SimpleDateFormat("MM");
SimpleDateFormat simpleDateFormatDay = new SimpleDateFormat("dd");
String year = simpleDateFormatYear.format(mDate);
String month = simpleDateFormatMonth.format(mDate);
String day = simpleDateFormatDay.format(mDate);
//String yearMonth = year + " " + month + " " + day + "";
String yearMonth = year + "-" + month + "-" + day;

return yearMonth;
}
}

 

위 소스를 긁어서 사용해도 되고 스틱코드에 자동완성 기능으로 첨부 해놓겠습니다.

 

 

 

getNowHour 메소드는 현재 시간을 받아오는 메소드입니다.

 

getNowTime은 년-월-일  시간:분:밀리초 형태로 밀리초까지의 현재 날짜를 알수 있는 메소드 입니다.

 

getNowYearMonth() 는 년과 월만 알고 싶을때 사용합니다.

 

getNowYearMonthDay는 년,월,일 에 대한 정보를 알고 싶을때 호출 할 수 있습니다.

 

 

 

스틱코드를 사용하면 코드를 언제어디서든 쉽게 불러 올 수 있습니다.

https://stickode.com/mainlogin.html

 

STICKODE

 

stickode.com