Java DateTimeFormatter parse(DateTimeFormatter formatter, String string)

Here you can find the source of parse(DateTimeFormatter formatter, String string)

Description

Parses a string into an Instant .

License

Open Source License

Parameter

Parameter Description
formatter Formatter to be used for the parsing operation
string Source String

Return

object parsed from the string passed as parameter

Declaration

public static Instant parse(DateTimeFormatter formatter, String string) 

Method Source Code


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

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAccessor;

public class Main {
    /**//from  w  w w . j  a  va 2s  .co m
     * Parses a string into an {@link Instant}. Approximations may be introduced
     * if the text passed as parameter does not represent an instant in time
     * down to a nanosecond resolution.
     *
     * @param formatter
     *            Formatter to be used for the parsing operation
     * @param string
     *            Source String
     * @return {@link Instant} object parsed from the string passed as parameter
     */
    public static Instant parse(DateTimeFormatter formatter, String string) {
        Instant now = null;
        int year = 0;
        int month = 0;
        int dayOfMonth = 0;
        int hour = 0;
        int minute = 0;
        int second = 0;
        int nanoOfSecond = 0;
        ZoneId zone = null;

        TemporalAccessor a = formatter.parse(string);
        if (a.isSupported(ChronoField.YEAR)) {
            year = a.get(ChronoField.YEAR);
        } else {
            now = Instant.now();
            year = now.get(ChronoField.YEAR);
        }

        if (a.isSupported(ChronoField.MONTH_OF_YEAR)) {
            month = a.get(ChronoField.MONTH_OF_YEAR);
        } else {
            if (now == null) {
                now = Instant.now();
            }
            month = now.get(ChronoField.MONTH_OF_YEAR);
        }

        if (a.isSupported(ChronoField.DAY_OF_MONTH)) {
            dayOfMonth = a.get(ChronoField.DAY_OF_MONTH);
        } else {
            if (now == null) {
                now = Instant.now();
            }
            dayOfMonth = now.get(ChronoField.DAY_OF_MONTH);
        }

        if (a.isSupported(ChronoField.HOUR_OF_DAY)) {
            hour = a.get(ChronoField.HOUR_OF_DAY);
        } else {
            if (now == null) {
                now = Instant.now();
            }
            hour = now.get(ChronoField.HOUR_OF_DAY);
        }

        if (a.isSupported(ChronoField.MINUTE_OF_HOUR)) {
            minute = a.get(ChronoField.MINUTE_OF_HOUR);
        } else {
            if (now == null) {
                now = Instant.now();
            }
            minute = now.get(ChronoField.MINUTE_OF_HOUR);
        }

        if (a.isSupported(ChronoField.SECOND_OF_MINUTE)) {
            second = a.get(ChronoField.SECOND_OF_MINUTE);
        } else {
            second = 0;
        }

        if (a.isSupported(ChronoField.NANO_OF_SECOND)) {
            second = a.get(ChronoField.NANO_OF_SECOND);
        } else {
            nanoOfSecond = 0;
        }

        if (a.isSupported(ChronoField.OFFSET_SECONDS)) {
            ZoneId.ofOffset("UTC", ZoneOffset.from(a));
        } else {
            if (now == null) {
                now = Instant.now();
            }
            zone = ZoneId.systemDefault();
        }

        return ZonedDateTime.of(year, month, dayOfMonth, hour, minute, second, nanoOfSecond, zone).toInstant();
    }
}

Related

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