java 日期转换类_Java时间转换类实现

public classDateUtil {public staticDate getDate(Date date){

Date dt= null;

Calendar calendar=Calendar.getInstance();

calendar.setTime(date);

calendar.set(Calendar.HOUR_OF_DAY,0);

calendar.set(Calendar.MINUTE,0);

calendar.set(Calendar.SECOND,0);

calendar.set(Calendar.MILLISECOND,0);

calendar.add(Calendar.DAY_OF_MONTH,1);

dt=calendar.getTime();returndt;

}public staticString getDateString(Date date){

String sDate= "";

SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");

Date dt=getDate(date);

sDate=sdf.format(dt);returnsDate;

}public static longgetDateLong(Date date){long lDate = 0;

Date dt=getDate(date);

lDate=dt.getTime();returnlDate;

}public static longgetDateTimeLong(Date date){long lDate = 0;

lDate=date.getTime();returnlDate;

}public staticString getString(Date date, String format){

String sDate="";

SimpleDateFormat sdf= newSimpleDateFormat(format);

sDate=sdf.format(date);returnsDate;

}public static longgetString(String dateTime, String format){long lDate = 0;

SimpleDateFormat sdf= newSimpleDateFormat(format);try{

Date date=sdf.parse(dateTime);

lDate=date.getTime();

}catch(ParseException e) {//TODO Auto-generated catch block

e.printStackTrace();

}returnlDate;

}public static String getString(longtm, String format){

String sDate="";

Date date=DateUtil.putDateTime(tm);

SimpleDateFormat sdf= newSimpleDateFormat(format);

sDate=sdf.format(date);returnsDate;

}public static Date putDateTime(longtime){

Date date= null;

date= newDate(time);returndate;

}public static Date putDate(longtime){

Date date= null;

date= newDate(time);

date=getDate(date);returndate;

}public staticDate putDateTime(String dtString, String format){

Date date= null;

SimpleDateFormat sdf= newSimpleDateFormat(format);try{

date=sdf.parse(dtString);

}catch(ParseException e) {//TODO Auto-generated catch block

e.printStackTrace();

}returndate;

}public static Date AddYear(Date date, inty){

Date dt= null;

Calendar calendar=Calendar.getInstance();

calendar.setTime(date);

calendar.add(Calendar.YEAR, y);

dt=calendar.getTime();returndt;

}public static Date AddMonth(Date date, intm){

Date dt= null;

Calendar calendar=Calendar.getInstance();

calendar.setTime(date);

calendar.add(Calendar.MONTH, m);

dt=calendar.getTime();returndt;

}public static Date AddDay(Date date, intd){

Date dt= null;

Calendar calendar=Calendar.getInstance();

calendar.setTime(date);

calendar.add(Calendar.DAY_OF_MONTH, d);

dt=calendar.getTime();returndt;

}public static Date AddHour(Date date, inth){

Date dt= null;

Calendar calendar=Calendar.getInstance();

calendar.setTime(date);

calendar.add(Calendar.HOUR_OF_DAY, h);

dt=calendar.getTime();returndt;

}public static Date AddMinute(Date date, intm){

Date dt= null;

Calendar calendar=Calendar.getInstance();

calendar.setTime(date);

calendar.add(Calendar.MINUTE, m);

dt=calendar.getTime();returndt;

}public static Date AddSecond(Date date, ints){

Date dt= null;

Calendar calendar=Calendar.getInstance();

calendar.setTime(date);

calendar.add(Calendar.SECOND, s);

dt=calendar.getTime();returndt;

}public static Date Add(Date date, intmillisecond){

Date dt= null;

Calendar calendar=Calendar.getInstance();

calendar.setTime(date);

calendar.add(Calendar.MILLISECOND, millisecond);

dt=calendar.getTime();returndt;

}public static longSubtrat(Date date1, Date date2){long l = 0;

l= date1.getTime() -date2.getTime();returnl;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值