Java String to Date toDate(String date)

Here you can find the source of toDate(String date)

Description

Returns a Java representation of a Facebook date string.

License

Open Source License

Parameter

Parameter Description
date Facebook date string.

Exception

Parameter Description
IllegalArgumentException If the provided date isn't in the Facebook date format(ISO 8601).

Return

Java date representation of the given Facebook date string.

Declaration

static Date toDate(String date) throws IllegalArgumentException 

Method Source Code


//package com.java2s;
/*/*from   w  w  w  .j  a v  a  2s  .  com*/
 * Copyright (c) 2010 Mark Allen.
 * 
 * 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.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    /**
     * Facebook date format (ISO 8601). Example: 2010-02-28T16:11:08+0000
     */
    private static final String FACEBOOK_DATE_FORMAT = "yyyy-MM-dd'T'kk:mm:ss";

    /**
     * Returns a Java representation of a Facebook {@code date} string.
     * 
     * @param date
     *          Facebook {@code date} string.
     * @return Java date representation of the given Facebook {@code date} string.
     * @throws IllegalArgumentException
     *           If the provided {@code date} isn't in the Facebook date format
     *           (ISO 8601).
     */
    static Date toDate(String date) throws IllegalArgumentException {
        if (date == null)
            return null;

        try {
            return new Date(Long.parseLong(date) * 1000);
        } catch (NumberFormatException e) {

        }

        try {
            return new SimpleDateFormat(FACEBOOK_DATE_FORMAT).parse(date);
        } catch (ParseException e) {
            throw new IllegalArgumentException(
                    "Unable to parse date '" + date + "' using format string '" + FACEBOOK_DATE_FORMAT + "'", e);
        }
    }
}

Related

  1. toDate(String date)
  2. toDate(String date)
  3. toDate(String date)
  4. toDate(String date)
  5. toDate(String date)
  6. toDate(String date)
  7. toDate(String date, String dateFormat)
  8. toDate(String date, String format)
  9. toDate(String date, String format)