首页 > 编程语言 >Java: Date and Time

Java: Date and Time

时间:2022-11-27 07:33:05浏览次数:38  
标签:Time Java LocalDateTime public yyyy time Date import class

Java does not have a built-in Date class, but we can import the java.time package to work with the date and time API.

ClassDescription
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

 

 

 

 

 

 

LocalDate example:

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
  }
}

// Outputs:
2022-11-27

 

LocalTime example:

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);
  }
}

// Outputs:
00:06:32.090087

 

LocalDateTime example:

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);
  }
}

// Outputs
2022-11-27T00:06:32.090780

 

Formatting Date and Time

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);
  }
}
// Outputs
Before Formatting: 2022-11-27T00:06:32.091782
After Formatting: 27-11-2022 00:06:32

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"
LocalDate

标签:Time,Java,LocalDateTime,public,yyyy,time,Date,import,class
From: https://www.cnblogs.com/ShengLiu/p/16928940.html

相关文章

  • Java: User Input (Scanner)
    The Scanner classisusedtogetuserinput,anditisfoundinthe java.util package.Tousethe Scanner class,createanobjectoftheclassandusean......
  • Java: Enums
    An enum isaspecial"class"thatrepresentsagroupof constants (unchangeablevariables,like final variables).enumLevel{LOW,MEDIUM,HIGH}......
  • Java: Interface
    Anotherwaytoachieve abstraction inJava,iswithinterfaces.An interface isacompletely"abstractclass"thatisusedtogrouprelatedmethodswithem......
  • Java: Inner Classes
    InJava,itisalsopossibletonestclasses(aclasswithinaclass).Thepurposeofnestedclassesistogroupclassesthatbelongtogether,whichmakesyour......
  • Java: Abstraction
    Abstractioncanbeachievedwitheither abstractclasses or interfaces.The abstract keywordisanon-accessmodifier,usedforclassesandmethods:Abst......
  • Java: Classes
    WhatareClassesandObjects?Aclassisatemplateforobjects,andanobjectisaninstanceofaclass.   Staticvs.Publicwecreateda static met......
  • Collectors groupingBy() method in Java with Examples
    https://www.geeksforgeeks.org/collectors-groupingby-method-in-java-with-examples/The groupingBy() methodofCollectorsclassinJavaareusedforgroupingo......
  • Java 8 | Collectors counting() with Examples
    https://www.geeksforgeeks.org/java-8-collectors-counting-with-examples/Collectorscounting() methodisusedtocountthenumberofelementspassedinthestr......
  • Stream In Java
    https://www.geeksforgeeks.org/stream-in-java/ IntroducedinJava8,theStreamAPIisusedtoprocesscollectionsofobjects.Astreamisasequenceofobje......
  • Comparable vs Comparator in Java
    https://www.geeksforgeeks.org/comparable-vs-comparator-in-java/ Javaprovidestwointerfacestosortobjectsusingdatamembersoftheclass:  Comparabl......