java에서 지금 시간을 원하는 형식(yyyy-MM-dd)로 출력하기 위한 코드입니다.

 

	private String getToday(){
			//데이트 객체 생성
		Date now = new Date();
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		
		return format.format(now).toString();
	}

 

'JAVA를 해보자' 카테고리의 다른 글

[java] controller에서 redirect 방법  (0) 2019.05.22
Java 시간계산 함수  (0) 2019.04.10
객체지향 vs 절차지향  (0) 2018.02.20
java_ 생성자  (0) 2018.01.08
enum_열거형  (0) 2018.01.08

현재시간으로부터 입력받은 시간을 계산 하여 문자열로 반환 하는 함수

param

 hour : 입력받은 시간

 format : 반환 형식

return : string type 문자열

	private String getThreeHoursAgoTime(int hour,String format) {
		String res = "";
		
		Date nowTime = new Date();
		
		// 포맷변경 ( 년월일시분) 
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);	
		res = simpleDateFormat.format(nowTime);
		
		res = res.substring(0, 11)+"0";
		
		// Java 시간 더하기 
		Calendar cal = Calendar.getInstance(); 
		cal.setTime(nowTime); 

		// 3시간 전 
		cal.add(Calendar.HOUR, hour); 
		res = simpleDateFormat.format(cal.getTime());
		
		//10분 단위로 반환
		res = res.substring(0, 11)+"0"; 
		
		return res;
	}

'JAVA를 해보자' 카테고리의 다른 글

[JAVA] simpleDateFormat yyyy-MM-dd 날짜 포멧  (0) 2019.05.23
[java] controller에서 redirect 방법  (0) 2019.05.22
객체지향 vs 절차지향  (0) 2018.02.20
java_ 생성자  (0) 2018.01.08
enum_열거형  (0) 2018.01.08

+ Recent posts