SoFunction
Updated on 2025-03-10

Java obtains the time code example of yyyy-MM-dd HH:mm:ss standard format based on the current time

In Java, you can use the LocalDateTime class and DateTimeFormatter class in the package to get and format the current time to yyyy-MM-dd HH:mm:ss format.

Code Example

Here is how to get the current time and format it asyyyy-MM-dd HH:mm:ssComplete example of format:

import ;
import ;

public class CurrentTimeExample {
    public static void main(String[] args) {
        // Get the current time        LocalDateTime currentTime = ();

        // Define the time format        DateTimeFormatter formatter = ("yyyy-MM-dd HH:mm:ss");

        // Format the current time        String formattedTime = (formatter);

        // Output result        ("Current time: " + formattedTime);
    }
}

illustrate

  • (): Get the current date and time.
  • ("yyyy-MM-dd HH:mm:ss"): Define the time format.
  • (formatter): Format the current time to the specified format.

Complete code

Here is the complete code, containing the necessary imports and explanations:

import ;
import ;

public class CurrentTimeExample {
    public static void main(String[] args) {
        // Get the current time        LocalDateTime currentTime = ();
        
        // Define the time format        DateTimeFormatter formatter = ("yyyy-MM-dd HH:mm:ss");
        
        // Format the current time        String formattedTime = (formatter);
        
        // Output result        ("Current time: " + formattedTime);
    }
}

Running results

Run the above code and you should see an output similar to the following:

Current time: 2024-07-16 12:34:56

Other formats

If you need another time format, you can change itDateTimeFormattermode. For example:

  • yyyy/MM/dd HH:mm:ss
  • dd-MM-yyyy HH:mm:ss
  • G 'at' HH:mm:ss z

Summarize

By usingPackage, which can easily get the current time and format it into different string representations. This package provides powerful date and time processing functions, making date and time operation more concise and intuitive.

Next step suggestions:a.Try to format other times (such as past or future times) to get familiar with itLocalDateTimevarious methods.
b.useZonedDateTimeHandle time zone issues and get the current time of different time zones.

Although the package (the modern date-time API introduced in Java 8) is the recommended way, the same functionality can be achieved if you need to use the traditional Date class.

Here is an example of using and to get the current time and formatting it into yyyy-MM-dd HH:mm:ss format.

Code Example

1. Use Date and SimpleDateFormat to get and format the current time

import ;
import ;

public class DateExample {
    public static void main(String[] args) {
        // Get the current time        Date now = new Date();

        // Define the time format        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // Format the current time        String formattedTime = (now);

        // Output result        ("Current time: " + formattedTime);
    }
}

illustrate

  • new Date(): Create a representative of the current date and timeDateObject.
  • SimpleDateFormat: Used to define date and time format.
  • (now): Format the current date and time to the specified string format.

Complete code

Here is the complete code, containing the necessary imports and explanations:

import ;
import ;

public class DateExample {
    public static void main(String[] args) {
        // Get the current time        Date now = new Date();
        
        // Define the time format        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        
        // Format the current time        String formattedTime = (now);
        
        // Output result        ("Current time: " + formattedTime);
    }
}

Running results

Run the above code and you should see an output similar to the following:

Current time: 2024-07-16 12:34:56

Things to note

  • SimpleDateFormatClasses are not thread-safe, so special care is required when used in multi-threaded environments.
  • AlthoughDateandSimpleDateFormatIt is a traditional method, but it is recommended to use it in modern Java development.Bag.

Other formats

If you need another time format, you can change itSimpleDateFormatmode. For example:

  • yyyy/MM/dd HH:mm:ss
  • dd-MM-yyyy HH:mm:ss
  • G 'at' HH:mm:ss z

Summarize

By usingand, can easily get the current time and format it into different string representations. This approach is suitable for situations where compatibility with older versions of Java or existing code is required.

Next step suggestions:a.Try to format other times (such as past or future times) to get familiar withDateandSimpleDateFormatVarious methods.
b.Use in multi-threaded environmentThreadLocalCome and make sureSimpleDateFormatThread safety.

This is the article about obtaining yyyy-MM-dd HH:mm:ss standard format time according to the current time of java. This is the end of this article. For more related java, obtaining yyyy-MM-dd HH:mm:ss format content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!