Java LocalDateTime Format convertDateStringToLocalDateTime(String formattedValue, int hour, int minute)

Here you can find the source of convertDateStringToLocalDateTime(String formattedValue, int hour, int minute)

Description

Method that takes a string already preformatted to conform to preferred date format, and returns a LocalDateTime that corresponds to that date, at a specific hour and minute

License

Open Source License

Parameter

Parameter Description
formattedValue The date string, pre-formatted to the pattern "dd-MM-yyyy"
hour The hour at which to set the local date time
minute The minute at which to set the local date time

Return

A local date time object representing the provided string at the specified hour and minute

Declaration

public static LocalDateTime convertDateStringToLocalDateTime(String formattedValue, int hour, int minute) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

import java.util.Objects;

public class Main {
    private static final DateTimeFormatter preferredDateFormat = DateTimeFormatter.ofPattern("dd-MM-yyyy");

    /**/*from  ww w .  j av  a2s  . c om*/
     * Method that takes a string already preformatted to conform to preferred date format, and returns a LocalDateTime
     * that corresponds to that date, at a specific hour and minute
     * @param formattedValue The date string, pre-formatted to the pattern "dd-MM-yyyy"
     * @param hour The hour at which to set the local date time
     * @param minute The minute at which to set the local date time
     * @return A local date time object representing the provided string at the specified hour and minute
     */
    public static LocalDateTime convertDateStringToLocalDateTime(String formattedValue, int hour, int minute) {
        Objects.requireNonNull(formattedValue);
        if ((hour < 0 || hour > 24) || (minute < 0 || minute > 60)) {
            throw new IllegalArgumentException("Illegal argument! Hours and minutes must be in range 0-24, 0-60");
        }
        try {
            final LocalDate parsedDate = LocalDate.parse(formattedValue, preferredDateFormat);
            return LocalDateTime.of(parsedDate, LocalTime.of(hour, minute));
        } catch (DateTimeParseException e) {
            throw new IllegalArgumentException("Illegal argument! Date string not formatted as dd-MM-yyyy");
        }
    }
}

Related

  1. convertTemporalToSplunkSearchFormat(OffsetDateTime localDateTime)
  2. format(LocalDateTime dateTime)
  3. format(LocalDateTime localDateTime, String pattern)
  4. formatDate(LocalDateTime dateTime)