Java Date Format ISO toISODate(String format, String value)

Here you can find the source of toISODate(String format, String value)

Description

Gets a ISO representation of a date within a string, when provided the format of the original string.

License

Open Source License

Parameter

Parameter Description
format the format of the original string
value the date time value within the string.

Return

an ISO representation of the string.

Declaration

public static String toISODate(String format, String value) 

Method Source Code

//package com.java2s;
/**// ww  w .  j a  v  a2 s.c o m
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
    
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
    
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**/

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    /**
     * The ISO date format string.
     */
    private static String isoDateStringFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'";

    /**
     * Gets a ISO representation of a date within a string, when provided the
     * format of the original string.
     * 
     * @param format
     *            the format of the original string
     * @param value
     *            the date time value within the string.
     * @return an ISO representation of the string.
     */
    public static String toISODate(String format, String value) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            Date date = sdf.parse(value);
            sdf.applyPattern(isoDateStringFormat);
            return sdf.format(date);
        } catch (ParseException e) {
        }
        return null;
    }

    public static String toISODate(Date date) {
        DateFormat df = new SimpleDateFormat(isoDateStringFormat);
        return df.format(date);
    }
}

Related

  1. toISO8601(Date date)
  2. toIso8601(Date dateObj)
  3. toIso8601(Date dateTime)
  4. toISO8601(Date pDate)
  5. toISO8601FileSystemCompatible(Calendar calendar)
  6. toISODateRealTimeFormat(Date iDate)
  7. toISOString(Date date, String format, TimeZone tz)
  8. toIsoTime(Date timestamp)
  9. toIsoTime(Date timestamp)