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")));
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值