parse string in format yyyy-MM-dd HH:mm:ss to LocalDateTime - Java java.time

Java examples for java.time:LocalDateTime

Description

parse string in format yyyy-MM-dd HH:mm:ss to LocalDateTime

Demo Code


//package com.java2s;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;

public class Main {
    public static void main(String[] argv) throws Exception {
        String text = "java2s.com";
        System.out.println(parse(text));
    }/*from   w w w .  ja  va 2 s  .co m*/

    private static final String PATTERN = "yyyy-MM-dd HH:mm:ss";
    private static final DateTimeFormatter FMT = new DateTimeFormatterBuilder()
            .appendPattern(PATTERN).toFormatter();

    public static LocalDateTime parse(String text) {
        LocalDateTime dt = null;
        if (text != null && text.length() == PATTERN.length()) {
            dt = LocalDateTime.parse(text, FMT);
        }
        return dt;
    }
}

Related Tutorials