java对日期的操作工具类

import java.io.PrintStream;
import java.sql.Date;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class SqlDateUtil
{

    private static Log log;
    public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";
    public static final String DEFAULT_TIEMSTAMP_PATTERN = "yyyy-MM-dd HH:mm:ss";
    static Class class$org$dragon$util$SqlDateUtil; /* synthetic field */

    public SqlDateUtil()
    {
    }

    public static String currentDateString()
    {
        return currentDateString("yyyy-MM-dd");
    }

    public static String currentDateString(String pattern)
    {
        SimpleDateFormat simpleFormat = new SimpleDateFormat(pattern);
        String result = simpleFormat.format(new Date(System.currentTimeMillis()));
        return result;
    }

    public static Date currentDate()
    {
        return new Date(System.currentTimeMillis());
    }

    public static Date parseDate(int year, int month, int day)
        throws DateException
    {
        validateDate(year, month, day);
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, day);
        Date date = new Date(calendar.getTimeInMillis());
        return date;
    }

    public static Date parseDate(String dateStr)
        throws DateException
    {
        String dateArr[] = dateStr.split("-");
        int year = Integer.parseInt(dateArr[0]);
        int month = Integer.parseInt(dateArr[1]);
        int day = Integer.parseInt(dateArr[2]);
        Date date = parseDate(year, month, day);
        return date;
    }

    public static String currentTimestampString()
    {
        return currentTimestampString("yyyy-MM-dd HH:mm:ss");
    }

    public static String currentTimestampString(String pattern)
    {
        SimpleDateFormat simpleFormat = new SimpleDateFormat(pattern);
        String result = simpleFormat.format(new Timestamp(System.currentTimeMillis()));
        return result;
    }

    public static Timestamp currentTimestamp()
    {
        return new Timestamp(System.currentTimeMillis());
    }

    public static Timestamp parseTimestamp(int year, int month, int day, int hour, int minute, int second)
        throws DateException
    {
        validateTimestamp(year, month, day, hour, minute, second);
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, day, hour, minute, second);
        Timestamp timestamp = new Timestamp(calendar.getTimeInMillis());
        return timestamp;
    }

    public static Timestamp parseTimestamp(String timestampStr, String pattern)
        throws DateException
    {
        SimpleDateFormat simpleFormat = new SimpleDateFormat(pattern);
        Timestamp Timestamp = null;
        try
        {
            Timestamp = new Timestamp(simpleFormat.parse(timestampStr).getTime());
        }
        catch(ParseException ex)
        {
            throw new DateException("", ex);
        }
        return Timestamp;
    }

    public static Timestamp parseTimestamp(String timestampStr)
        throws DateException
    {
        return parseTimestamp(timestampStr, "yyyy-MM-dd");
    }

    private static void validateTimestamp(int year, int month, int day, int hour, int minute, int second)
        throws DateException
    {
        if(year < 1900 || year > 2400)
            throw new DateException("Date parse error: the year is invalid.");
        if(month < 1 || month > 12)
            throw new DateException("Date parse error: the month is invalid.");
        if(day < 1 || day > 31)
            throw new DateException("Date parse error: the day is invalid.");
        if(hour < 0 || hour > 23)
            throw new DateException("Date parse error: the day is invalid.");
        if(minute < 0 || minute > 59)
            throw new DateException("Date parse error: the day is invalid.");
        if(second < 0 || second > 59)
            throw new DateException("Date parse error: the day is invalid.");
        else
            return;
    }

    private static void validateDate(int year, int month, int day)
        throws DateException
    {
        if(year < 1900 || year > 2400)
            throw new DateException("Date parse error: the year is invalid.");
        if(month < 1 || month > 12)
            throw new DateException("Date parse error: the month is invalid.");
        if(day < 1 || day > 31)
            throw new DateException("Date parse error: the day is invalid.");
        else
            return;
    }

    public static void main(String args[])
    {
        SqlDateUtil sqlDateUtil = new SqlDateUtil();
        String str = "IS_DELETED";
        try
        {
            System.out.println("Date:" + sqlDateUtil.parseDate("2005-04-5"));
        }
        catch(DateException ex)
        {
            ex.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar.getTime());
    }

    static Class class$(String x0)
    {
        return Class.forName(x0);
        ClassNotFoundException x1;
        x1;
        throw new NoClassDefFoundError(x1.getMessage());
    }

    static
    {
        log = LogFactory.getLog(class$org$dragon$util$SqlDateUtil != null ? class$org$dragon$util$SqlDateUtil : (class$org$dragon$util$SqlDateUtil = class$("org.dragon.util.SqlDateUtil")));
    }
}

你可能感兴趣的:(java,apache,sql)