Parses a String to a LocalDateTime object like for example to '2015-12-15 12:30'; - Java java.time

Java examples for java.time:LocalDateTime

Description

Parses a String to a LocalDateTime object like for example to '2015-12-15 12:30';

Demo Code


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

public class Main {
    /**/*  w  w  w .  j a va  2s . c o m*/
     * Parses a String to a LocalDateTime object like for example to '2015-12-15 12:30';
     * 
     * @param dateAndTime
     *          The String that should be formatted and parsed to a LocalDateTime object.
     * @return The formatted LocalDateTime Object of the passed String.
     */
    public static LocalDateTime formatDateAndTime(String dateAndTime) {
        DateTimeFormatter formatter = DateTimeFormatter
                .ofPattern("yyyy-MM-dd HH:mm");
        LocalDateTime formatedDate = LocalDateTime.parse(dateAndTime,
                formatter);
        return formatedDate;
    }
}

Related Tutorials