Java Date Create toDate(long time)

Here you can find the source of toDate(long time)

Description

Returns the given long value as a date object.

License

Open Source License

Declaration

private static Date toDate(long time) 

Method Source Code

//package com.java2s;
/**/*ww w  . jav a2s  .co  m*/
 * com.mckoi.database.global.CastHelper  11 Oct 2001
 *
 * Mckoi SQL Database ( http://www.mckoi.com/database )
 * Copyright (C) 2000, 2001, 2002  Diehl and Associates, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * Version 2 as published by the Free Software Foundation.
 *
 * 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
 * GNU General Public License Version 2 for more details.
 *
 * You should have received a copy of the GNU General Public License
 * Version 2 along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Change Log:
 * 
 * 
 */

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

public class Main {
    /**
     * Date, Time and Timestamp parser/formatters
     */
    private static DateFormat[] date_format_sql;

    /**
     * Returns the given long value as a date object.
     */
    private static Date toDate(long time) {
        return new Date(time);
    }

    /**
     * Parses a String as an SQL date.
     */
    public static Date toDate(String str) {
        synchronized (date_format_sql) {
            for (int i = 0; i < date_format_sql.length; ++i) {
                try {
                    return date_format_sql[i].parse(str);
                } catch (ParseException e) {
                }
            }
            throw new RuntimeException(dateErrorString("Unable to parse string as a date ", date_format_sql));
        }
    }

    /**
     * Helper that generates an appropriate error message for a date format error.
     */
    private static String dateErrorString(String msg, DateFormat[] df) {
        String pattern = "";
        if (df[0] instanceof SimpleDateFormat) {
            SimpleDateFormat sdf = (SimpleDateFormat) df[0];
            pattern = "(" + sdf.toPattern() + ")";
        }
        return msg + pattern;
    }
}

Related

  1. toDate(int date)
  2. toDate(int flag, Date... date)
  3. toDate(long lastModified)
  4. toDate(long segundos)
  5. toDate(long time)
  6. toDate(Object o)
  7. ToDate(Object o)
  8. toDate(Object object)
  9. toDate(Object value)