SoFunction
Updated on 2025-04-14

Detailed explanation of Java implementation time and string conversion

1. Date formatted as string

In Java, you can use the DateTimeFormatter class in the package to format the date as a string. Here is an example using DateTimeFormatter:

(I) Use predefined format

import ;
import ;

public class DateFormatExample {
    public static void main(String[] args) {
        LocalDateTime now = ();
        DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
        String formattedDate = (formatter);
        ("ISO format date and time: " + formattedDate);
    }
}

(II) Custom format

import ;
import ;

public class CustomFormatExample {
    public static void main(String[] args) {
        LocalDateTime now = ();
        DateTimeFormatter formatter = ("yyyy-MM-dd HH:mm:ss");
        String formattedDate = (formatter);
        ("Custom format date and time: " + formattedDate);
    }
}

2. String parses into date

When parsing a string to a date, you need to make sure that the format of the string is consistent with the format specified by DateTimeFormatter. Here is an example of parsing a string as a date:

(I) Parsing ISO format string

import ;
import ;

public class ParseISODateExample {
    public static void main(String[] args) {
        String isoDate = "2023-10-11T12:34:56";
        DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
        LocalDateTime date = (isoDate, formatter);
        ("Date and time after parsing: " + date);
    }
}

(II) Parsing custom format strings

import ;
import ;

public class ParseCustomDateExample {
    public static void main(String[] args) {
        String customDate = "2023-10-11 12:34:56";
        DateTimeFormatter formatter = ("yyyy-MM-dd HH:mm:ss");
        LocalDateTime date = (customDate, formatter);
        ("Date and time after parsing: " + date);
    }
}

3. Process dates in different time zones

When dealing with dates in different time zones, you can use the ZonedDateTime class. Here is an example of converting a date to a different time zone:

import ;
import ;
import ;
import ;

​​​​​​​public class TimeZoneExample {
    public static void main(String[] args) {
        LocalDateTime now = ();
        ZoneId zoneId = ("America/New_York");
        ZonedDateTime zonedDateTime = (now, zoneId);
        DateTimeFormatter formatter = ("yyyy-MM-dd HH:mm:ss z");
        String formattedDate = (formatter);
        ("New York Time Zone Date Time: " + formattedDate);
    }
}

4. Summary

Java's package provides powerful date and time processing capabilities, and can easily convert between dates and strings through DateTimeFormatter.

5. Method supplement

java time to string

To convert time in Java to a string, you can use a class or class to represent time and use a class to format the time.

Here is the example code for using and:

import ;
import ;
 
public class Main {
    public static void main(String[] args) {
        Date currentDate = new Date();
 
        // Specify date format        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 
        // Convert Date to string        String dateString = (currentDate);
 
        // Print the result        (dateString);
    }
}

Here is the example code for using and:

import ;
import ;
 
public class Main {
    public static void main(String[] args) {
        LocalDateTime currentDateTime = ();
 
        // Specify date format        DateTimeFormatter formatter = ("yyyy-MM-dd HH:mm:ss");
 
        // Convert LocalDateTime to String        String dateTimeString = (formatter);
 
        // Print the result        (dateTimeString);
    }
}

The date format in the above example is "yyyy-MM-dd HH:mm:ss", you can customize the date format as you want.

Java time to string conversion

1. Convert long string to yyyy-MM-dd HH:mm:ss format output

import ;  
import ;  
//Convert long string to format time outputpublic class LongToString {  
public static void main(String argsp[]){  
    String time="1256006105375";  
  
    Date date=new Date((time));  
    SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
    time=(date);  
    (time);  
}  
}  

2. Convert string to time

import ;  
import ;  
  
import ;  
  
public class StringToDate {  
public static void main(String argsp[]) throws Exception{  
    String time="2010-11-20 11:10:10";  
  
    Date date=null;  
    SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
    date=(time);  
    (date);  
}  
}  

3. Get the current system time and return the yyyy-MM-dd HH:mm:ss string

import ;  
import ;  
  
public class StringToDate {  
public static void main(String argsp[]) throws Exception{  
      
    Date date=new Date();  
    SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
    String time=(date);  
    (time);  
}  
}  

4. Get the current system time and return the HH:mm:ss string

import ;  
import ;  
  
public class StringToDate {  
public static void main(String argsp[]) throws Exception{  
      
    Date date=new Date();  
    SimpleDateFormat formatter=new SimpleDateFormat("HH:mm:ss");  
    String time=(date);  
    (time);  
}  
}  

5. Convert 20101125102503 to 2010-11-25 10:25:03 output

import ;  
import ;  
public class StringToDate {  
public static void main(String argsp[]) throws Exception{  
    String time="20101125102503";  
    SimpleDateFormat formatter1=new SimpleDateFormat("yyyy-HH-dd HH:mm:ss");  
    SimpleDateFormat formatter2=new SimpleDateFormat("yyyyHHddHHmmss");  
    time=((time));  
    (time);  
}  
}  

This is the end of this article about the detailed explanation of Java implementation time and string conversion. For more related contents of Java time and string conversion, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!