JAVA

Java - 날짜(Date), 시간(Time)

상상날개 2022. 8. 8. 14:10

https://www.w3schools.com/java/java_date.asp 

 

Java Date and Time

W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

www.w3schools.com

 

Class DescriptionDescription
LocalDate Represents a date (year, month, day (yyyy-MM-dd))
LocalTime Represents a time (hour, minute, second and nanoseconds (HH-mm-ss-ns))
LocalDateTime Represents both a date and a time (yyyy-MM-dd-HH-mm-ss-ns)
DateTimeFormatter Formatter for displaying and parsing date-time objects

 

1. 현재 날짜 표시

  • 현재 날짜를 표시하려면 java.time.LocalDate클래스를 가져오고 해당 now()메서드를 사용합니다.
import java.time.LocalDate; // import the LocalDate class

public class Main {
  public static void main(String[] args) {
    LocalDate myObj = LocalDate.now(); // Create a date object
    System.out.println(myObj); // Display the current date
  }
}


2022-08-08

 

2. 현재 시간 표시

  • 현재 시간(시, 분, 초 및 나노초)을 표시하려면 java.time.LocalTime클래스를 가져오고 해당 now()메서드를 사용합니다.
import java.time.LocalTime; // import the LocalTime class

public class Main {
  public static void main(String[] args) {
    LocalTime myObj = LocalTime.now();
    System.out.println(myObj);
  }
}


13:54:38.126484

 

 

3. 현재 날짜 및 시간 표시

  • 현재 날짜와 시간을 표시하려면 java.time.LocalDateTime클래스를 가져오고 해당 now()메서드를 사용합니다.
import java.time.LocalDateTime; // import the LocalDateTime class

public class Main {
  public static void main(String[] args) {
    LocalDateTime myObj = LocalDateTime.now();
    System.out.println(myObj);
  }
}


2022-08-08T13:54:38.126501

 

4. 날짜 및 시간 형식 지정

  • 위의 예에서 "T"는 날짜와 시간을 구분하는 데 사용됩니다. DateTimeFormatter동일한 패키지 의 메서드와 함께 클래스를 사용하여 ofPattern()날짜-시간 개체의 형식을 지정하거나 구문 분석할 수 있습니다. 다음 예는 날짜-시간에서 "T"와 나노초를 모두 제거합니다.
import java.time.LocalDateTime; // Import the LocalDateTime class
import java.time.format.DateTimeFormatter; // Import the DateTimeFormatter class

public class Main {
  public static void main(String[] args) {
    LocalDateTime myDateObj = LocalDateTime.now();
    System.out.println("Before formatting: " + myDateObj);
    DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");

    String formattedDate = myDateObj.format(myFormatObj);
    System.out.println("After formatting: " + formattedDate);
  }
}


Before Formatting: 2022-08-08T13:54:38.126996
After Formatting: 08-08-2022 13:54:38

 

5. ofPattern()

  • 다른 형식으로 날짜와 시간을 표시하려는 경우 이 메서드는 모든 종류의 값을 허용합니다
Value Example
yyyy-MM-dd "1988-09-29"
dd/MM/yyyy "29/09/1988"
dd-MMM-yyyy "29-Sep-1988"
E, MMM dd yyyy "Thu, Sep 29 1988"
import java.time.LocalDateTime;  // Import the LocalDateTime class
import java.time.format.DateTimeFormatter;  // Import the DateTimeFormatter class

public class Main {
  public static void main(String[] args) {  
    LocalDateTime myDateObj = LocalDateTime.now();  
    System.out.println("Before formatting: " + myDateObj);  
    DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");  
    
    String formattedDate = myDateObj.format(myFormatObj);  
    System.out.println("After formatting: " + formattedDate);  
  }  
}  



Before Formatting: 2022-08-08T14:05:58.945620
After Formatting: 08-08-2022 14:05:58
import java.time.LocalDateTime;  // Import the LocalDateTime class
import java.time.format.DateTimeFormatter;  // Import the DateTimeFormatter class

public class Main {
  public static void main(String[] args) {  
    LocalDateTime myDateObj = LocalDateTime.now();  
    System.out.println("Before Formatting: " + myDateObj);  
    DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");  
    
    String formattedDate = myDateObj.format(myFormatObj);  
    System.out.println("After Formatting: " + formattedDate);  
  }  
}  



Before Formatting: 2022-08-08T14:06:58.846495
After Formatting: 08/08/2022 14:06:58
import java.time.LocalDateTime;  // Import the LocalDateTime class
import java.time.format.DateTimeFormatter;  // Import the DateTimeFormatter class

public class Main {
  public static void main(String[] args) {  
    LocalDateTime myDateObj = LocalDateTime.now();  
    System.out.println("Before Formatting: " + myDateObj);  
    DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern("dd-MMM-yyyy HH:mm:ss");  
    
    String formattedDate = myDateObj.format(myFormatObj);  
    System.out.println("After Formatting: " + formattedDate);  
  }  
}  



Before Formatting: 2022-08-08T14:07:39.733365
After Formatting: 08-Aug-2022 14:07:39
import java.time.LocalDateTime;  // Import the LocalDateTime class
import java.time.format.DateTimeFormatter;  // Import the DateTimeFormatter class

public class Main {
  public static void main(String[] args) {  
    LocalDateTime myDateObj = LocalDateTime.now();  
    System.out.println("Before Formatting: " + myDateObj);  
    DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern("E, MMM dd yyyy HH:mm:ss");  
    
    String formattedDate = myDateObj.format(myFormatObj);  
    System.out.println("After Formatting: " + formattedDate);  
  }  
}  



Before Formatting: 2022-08-08T14:08:27.450224
After Formatting: Mon, Aug 08 2022 14:08:27