Java Date ISO Parse parseIsoDate(String str)

Here you can find the source of parseIsoDate(String str)

Description

Converts an ISO date or datetime string to a date object.

License

Open Source License

Parameter

Parameter Description
str the string to convert

Return

the date object, or null if the format wasn't recognized

Declaration

public static Date parseIsoDate(String str) 

Method Source Code


//package com.java2s;
/*//  w  w w.ja  va 2 s .  c o  m
 * RapidContext <http://www.rapidcontext.com/>
 * Copyright (c) 2007-2009 Per Cederberg & Dynabyte AB.
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the BSD license.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * See the RapidContext LICENSE for more details.
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    /**
     * The ISO date format.
     */
    private static final SimpleDateFormat ISO_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
    /**
     * The ISO date and time format.
     */
    private static final SimpleDateFormat ISO_DATETIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * Converts an ISO date or datetime string to a date object.
     * Note that this method will omit the time portion of the
     * string if not available.
     *
     * @param str            the string to convert
     *
     * @return the date object, or
     *         null if the format wasn't recognized
     */
    public static Date parseIsoDate(String str) {
        Date date;

        try {
            date = ISO_DATETIME_FORMAT.parse(str);
            if (formatIsoDateTime(date).equals(str)) {
                return date;
            }
        } catch (ParseException e) {
            // Do nothing, try with another date format
        }
        try {
            date = ISO_DATE_FORMAT.parse(str);
            if (formatIsoDate(date).equals(str)) {
                return date;
            }
        } catch (ParseException e) {
            // Do nothing, try with another date format
        }
        return null;
    }

    /**
     * Formats a date and time to an ISO datetime representation
     * (without timezone).
     *
     * @param date           the date and time to convert
     *
     * @return the ISO datetime string
     */
    public static String formatIsoDateTime(Date date) {
        if (date == null) {
            return null;
        } else {
            return ISO_DATETIME_FORMAT.format(date);
        }
    }

    /**
     * Formats a date to an ISO date representation. Note that the
     * time component of the date value will be ignored.
     *
     * @param date           the date to convert
     *
     * @return the ISO date string
     */
    public static String formatIsoDate(Date date) {
        if (date == null) {
            return null;
        } else {
            return ISO_DATE_FORMAT.format(date);
        }
    }
}

Related

  1. parseIsoDate(@Nonnull String asString)
  2. parseIsoDate(final String isoDateString)
  3. parseIsoDate(final String string)
  4. parseISODate(String date)
  5. parseISODate(String date)
  6. parseIsoDate2(String s)
  7. parseIsoDateAndTimeString(String aDateString, String aTimeString)
  8. parseIsoDateInput(String str)
  9. parseIsoDateTime(String isoDateTime)