首页 > 代码库 > Java经典实例:把字符串解析为日期时间

Java经典实例:把字符串解析为日期时间

Java版本:1.8开始

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * Created by Frank
 */
public class DateParse {
    public static void main(String[] args) {
        String armisticeDate  = "1914-11-11";
        LocalDate alD = LocalDate.parse(armisticeDate);
        System.out.println("Date:" + alD);

        String armisticeDateTime = "1914-11-11T11:11";
        LocalDateTime aLDT = LocalDateTime.parse(armisticeDateTime);
        System.out.println("Date/Time:" + aLDT);
    }
}

运行输出:

Date:1914-11-11
Date/Time:1914-11-11T11:11

 

Java经典实例:把字符串解析为日期时间