Java Date to LocalDateTime toLocalDateTime(String dateValue, String timeValue)

Here you can find the source of toLocalDateTime(String dateValue, String timeValue)

Description

This method parse date and time from String objects to LocalDateTime object.

License

Apache License

Parameter

Parameter Description
dateValue Data value in format "MM/dd/yyyy".
timeValue Time value in format "K:ma" - "4:00pm".

Return

Parsed date and time to LocalDateTime object.

Declaration

public static LocalDateTime toLocalDateTime(String dateValue, String timeValue) 

Method Source Code


//package com.java2s;
/*//from  ww w  .j a va  2s  .co m
 * Copyright 2015 hilemz
 *
 * http://www.wykop.pl/ludzie/hilemz/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

import java.time.format.DateTimeFormatter;

public class Main {
    /**
     * This method parse date and time from String objects to LocalDateTime object.
     * 
     * @param dateValue
     *            Data value in format "MM/dd/yyyy".
     * @param timeValue
     *            Time value in format "K:ma" - "4:00pm".
     * @return Parsed date and time to LocalDateTime object.
     */
    public static LocalDateTime toLocalDateTime(String dateValue, String timeValue) {
        return LocalDateTime.of(toLocalDate(dateValue), toLocalTime(timeValue));
    }

    /**
     * This method parse date from String object to LocalDate object.
     *
     * @param dateValue
     *            Data value in format "MM/dd/yyyy".
     * @return Parsed date to LocalDate object.
     */
    public static LocalDate toLocalDate(String dateValue) {
        return LocalDate.parse(trimQuotes(dateValue), DateTimeFormatter.ofPattern("M/dd/yyyy"));
    }

    /**
     * This method parse time from String object to LocalTime object.
     *
     * @param timeValue
     *            Time value in format "h:ma" - "4:00pm".
     * @return Parsed time to LocalTime object.
     */
    public static LocalTime toLocalTime(String timeValue) {
        return LocalTime.parse(trimQuotes(timeValue).toUpperCase(), DateTimeFormatter.ofPattern("h:ma"));
    }

    /**
     * This method trim " from passed value.
     * 
     * @param value
     *            Value to trim.
     * @return Trimmed value.
     */
    public static String trimQuotes(String value) {
        if (!isParsable(value)) {
            throw new IllegalArgumentException("Illegal argument value.");
        }
        return value.replaceAll("^\"|\"$", "");
    }

    /**
     * This method verificate if passed value is appropriate for parsing.
     *
     * @param value
     *            Value to check.
     * @return Result of verification.
     */
    public static boolean isParsable(String value) {
        return !(value == null || value.equals("N/A") || value.equals("") || value.equals("\"\""));
    }
}

Related

  1. toLocalDateTime(Date date)
  2. toLocalDateTime(Date dateTime)
  3. toLocalDateTime(Date utilDate)
  4. toLocalDateTime(long timestamp)
  5. toLocalDateTime(String date, int time)
  6. toLocalDateTime(String string)