Java DateTimeFormatter parse(String _date, String _format)

Here you can find the source of parse(String _date, String _format)

Description

Get an Instant from string at local zone

License

Open Source License

Return

the corresponding Instant

Declaration

public static final Instant parse(String _date, String _format) 

Method Source Code

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

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;

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

public class Main {
    public static final ZoneOffset localZoneOffset = ZoneOffset.UTC;

    /**/*from  www .ja  v a  2s.c  o m*/
     * Get an Instant from string at local zone
     * 
     * @return the corresponding Instant
     */
    public static final Instant parse(String _date, String _format) {
        DateTimeFormatter format = DateTimeFormatter.ofPattern(_format);

        try {
            return LocalDateTime.parse(_date, format).toInstant(localZoneOffset);
        } catch (DateTimeParseException e) {
            try {
                return LocalDate.parse(_date, format).atStartOfDay().toInstant(localZoneOffset);
            } catch (DateTimeParseException e2) {
                throw e2;
            }
        }
    }
}

Related

  1. getTimeFromEpochMilli(String value, boolean shortFormat, String errorText)
  2. getTimeStamp(final String dateTimeFormatPattern)
  3. getTimestampedPath(Path baseFile, TemporalAccessor time, DateTimeFormatter formatter)
  4. parse(DateTimeFormatter formatter, String string)
  5. parse(final DateTimeFormatter formatter, final String value, final ZoneId zoneId)
  6. parse(String format, String datetime)
  7. parseAndFormat(String date, DateTimeFormatter fromFormatter, DateTimeFormatter toFormatter)
  8. parseDateTime(String dateTimeStr, String format)
  9. parseZoneDateTime(String value, DateTimeFormatter formatter)