java中⾃定义⽇期类_java⽇期操作⾃定义类
每次当需要对⽇期进⾏处理的时候,总是会记不住库、⽅法和格式,所以就⾃⼰简单封装了下;包含了⼤部分功能,部分不常⽤的没有实现。
package com.tunicorn.marketing.utils;
import java.sql.Timestamp;
ParseException;
SimpleDateFormat;
import java.util.Date;
public class DateUtils {
public static final int YEAR = 9;
public static final int HYEAR = 8;
public static final int QUARTER = 7;
public static final int MONTH = 6;
public static final int WEEK = 5;
public static final int DAY = 4;
public static final int HOUR = 3;
public static final int MINUTE = 2;
public static final int SECOND = 1;
private Date date;
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/*
*
*
* 对象实例化
*
*
*/
public DateUtils(Date date){
this.date = date;
}
public DateUtils(Timestamp timeStamp){
try {
this.date = timeStamp;
} catch (Exception e) {
e.printStackTrace();
}
}
public DateUtils(String dateStr){
try {
this.date = sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
}
public DateUtils(long milliseconds){
this.date = new Date(milliseconds);
}
/*
*
*
* ⽇期类型转换相关⽅法
*
*
*/
public Date toDate(){
return this.date;
}
public Timestamp toTimeStamp(){
return new Timestamp(Time());
}
public String toDateFormat(){
return this.sdf.format(this.date);
}
public String toDateFormat(String format){ SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(this.date);
}
public long toMilliSeconds(){
/*
*
*
* 获取时间段⽅法
*
*
*/
public long getTimeMillis(){
return Time();
}
java时间日期格式转换public String getDate(String format){ DateFormat(format);
}
public String getDate(){
DateFormat("yyyy-MM-dd"); }
public String getTime(){
DateFormat("HH:mm:ss"); }
public String getDateTime(){
DateFormat();
}
public long getYear(){
return Year();
}
public long getMonth(){
return Month() + 1;
}
public long getDay(){
return Day();
}
public long getHours(){
public long getMinutes(){
return Minutes();
}
public long getSeconds(){
return Seconds();
}
/*
*
*
* 设置时间段⽅法
*
*
*/
//TODO:
/*
*
*
* ⽇期加减运算相关⽅法
*
*
*/
public void add(long type, long num){ switch(String.valueOf(type)){
case "1" :
this.addYear(num);
break;
case "2" :
this.addHyear(num);
break;
case "3" :
this.addQuarter(num);
break;
case "4" :
this.addMonth(num);
break;
case "5" :
this.addWeek(num);
break;
case "6" :
this.addDay(num);
break;
case "7" :
this.addHours(num);
break;
case "8" :
this.addMinutes(num);
break;
case "9" :
this.addSeconds(num);
break;
}
}
/*
*
* GregorianCalendar gc=new GregorianCalendar();
* gc.setTime(new Date);
* gc.add(field,value);
* value为正则往后,为负则往前
* field取1加1年,取2加⽉,取3加周,取5加⼀天
* gc.(Calendar.YEAR),gc.get(Calendar.MONTH),gc.get(Calendar.DATE)); * return sf.Time());
*
*/
public void addSeconds(long num){
}
public void addMinutes(long num){
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论