Java DateTimeFormatter getDateFromFormat(String dtStr)

Here you can find the source of getDateFromFormat(String dtStr)

Description

Expects date string as MM-dd-yyyy

License

Open Source License

Parameter

Parameter Description
dtStr a parameter

Exception

Parameter Description
Exception an exception

Declaration

public static Date getDateFromFormat(String dtStr) throws Exception 

Method Source Code


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

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

import java.util.Date;

public class Main {
    /**/*from w w  w .j  a  va2 s . co m*/
     * Date formatter for MM-dd-yyyy
     */
    private static final DateTimeFormatter df2 = DateTimeFormatter.ofPattern("MM-dd-yyyy");

    /**
     * Expects date string as MM-dd-yyyy
     *
     * @param dtStr
     * @return
     * @throws Exception
     */
    public static Date getDateFromFormat(String dtStr) throws Exception {
        LocalDate ld = LocalDate.parse(dtStr, df2);
        return localDateToSystemAdjustedStartOfDayDate(ld);
    }

    /**
     * Adjusts a LocalDate to Midnight of the say day (start of day).
     *
     * @param d
     * @return
     * @throws Exception
     */
    public static Date localDateToSystemAdjustedStartOfDayDate(LocalDate d) throws Exception {
        LocalDateTime ldt = d.atStartOfDay();
        return getSystemDefaultDate(ldt);
    }

    /**
     * Get local system Date from LocalDateTime
     *
     * @param ldt
     * @return
     * @throws Exception
     */
    public static Date getSystemDefaultDate(LocalDateTime ldt) throws Exception {
        return Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant());
    }
}

Related

  1. formatTimestap(final long timestap)
  2. fromInfluxDBTimeFormat(final String timestamp)
  3. fromString(String dateString, DateTimeFormatter formatter)
  4. getDateFormat()
  5. getDateFormat(final Locale LOCALE)
  6. getDateTimeAttributeTimeFormat()
  7. getDateTimeFormatter()
  8. getFormatDate(String format)
  9. getFormattedDate()