본문 바로가기
개발~/JAVA

[JAVA] java.util.Date

by 보배곰 2017. 3. 17.

Java SE 7 

java.util 패키지에 Date 클래스 

https://docs.oracle.com/javase/7/docs/api/


java.util.Date


public class Date

extends Object

implements Serializable, Cloneable, Comparable<Date>


The class Date represents a specific instant in time, with millisecond precision. 

Date 클래스는 밀리세컨드(1000분의 1초) 정확도를 가지고 특정시간을 나타냅니다.


Deprecated된 메서드가 꽤 많네요. Deprecated 같은 경우 보안, 성능, 대체 가능성 등 여러 기준에 의해 메서드의 사용을 지양한다는 의미입니다. 언제 사라질지 모르는 메서드죠. 


api문서를 살펴보면(위의 url) Deprecated된 메서드 대신해서 쓸 수 있는 메서드들도 같이 표시하고 있습니다. 


Constructors (생성자)

 Constructor

 Description 

 Date()

 Allocates a Date object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond.

 Date(long date)

  Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.


Methods (메서드)

 Modifier and Type

 Method

 Description 

 boolean 

 after(Date when) 

 Tests if this date is after the specified date. 

 boolean 

 before(Date when) 

 Tests if this date is before the specified date.

 Object

 clone() 

 Return a copy of this object.

 int

 compareTo(Date anotherDate) 

 Compares two Dates for ordering.

 boolean

 equals(Object obj) 

 Compares two dates for equality. 

 long

 getTime() 

 Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.

 int

 hashCode() 

 Returns a hash code value for this object.

 void

 setTime(long time) 

 Sets this Date object to represent a point in time that is time milliseconds after January 1, 1970 00:00:00 GMT. 

 String

 toString() 

 Converts this Date object to a String of the form:



1. 오늘 날짜를 구하고 싶다면? 

Date now = new Date();
System.out.println(now);
System.out.println(new Date());



2. 오늘로부터 며칠 뒤(inputDay)의 날짜를 구하고 싶다면? 

long inputDay = 3;
Date now = new Date();
Date later = new Date(now.getTime() + (inputDay * 1000 * 24 * 60 * 60));
System.out.println("오늘 날짜: "+now);
System.out.println(inputDay + "후 날짜: " + later);



3. 두 날의 차이를 알고 싶다면? 

2번 식 변형

		
long inputDay = 3;
Date now = new Date();
Date later = new Date(now.getTime() + (inputDay * 1000 * 24 * 60 * 60));
		
long difference = later.getTime() - now.getTime();
long differDay = difference / 24 / 60 / 60 / 1000; 
System.out.println(differDay + "일");


4. 날짜의 표현 방법을 바꾸고 싶다면? 

Date now = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); //기본) 날짜 사이에 - System.out.println(formatter.format(now)); SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss"); // 24시간 형식 표현법 System.out.println(formatter2.format(now)); SimpleDateFormat formatter3 = new SimpleDateFormat("yyyy.MM.dd hh:mm:ss"); //기본) 날짜 사이에 . System.out.println(formatter3.format(now)); SimpleDateFormat formatter4 = new SimpleDateFormat("yyyy.MM.dd hh:mm:ss a"); //기본) 오전, 오후 표기 System.out.println(formatter4.format(now));


'개발~ > JAVA' 카테고리의 다른 글

Kotlin 소개  (0) 2022.12.10
[Spring boot] page 1부터 시작하기  (0) 2020.04.30
Log4j 사용하기  (0) 2019.12.19
JSON 빈 객체를 단말에 어떻게 내려줄까?(Android,iOS)  (0) 2019.04.25
주석이쁘게 달기  (0) 2017.06.16

댓글