java处理日期的类_Java实现的日期处理类完整实例

本文实例讲述了java实现的日期处理类。分享给大家供大家参考,具体如下:

开发中常常要使用日期,先小结如下,以备后用。

import java.text.parseexception;

import java.text.simpledateformat;

import java.util.calendar;

import java.util.date;

public class formattime {

private final static simpledateformat sdf=new simpledateformat("yyyymmdd");

private final static simpledateformat sdfymdhm = new simpledateformat("yyyymmddhhmmss");

private final static simpledateformat sdfymdhms = new simpledateformat("yyyy-mm-dd hh:mm:ss");

/**

*

* @title: getcurrentday

* @description: todo 获取当天时间(20161109)

* @return

*/

public static string getcurrentday(){

return sdf.format(new date());

}

/**

*

* @title: ftime2

* @description: todo 获取time这个日期以前dayago天的日期

* @return

*/

public static string ftime(string time,int dayago){

date date = null;

try {

date = sdf.parse(time);

} catch (parseexception e) {

throw new runtimeexception(e);

}

calendar calendar = calendar.getinstance();

calendar.settime(date);

if(dayago>0){

calendar.add(calendar.day_of_month, -dayago);//前15天数据

date = calendar.gettime();

calendar.settime(date);

}

int year=calendar.get(calendar.year);

int month=calendar.get(calendar.month) + 1;

int day = calendar.get(calendar.day_of_month);

string mon="";

string d="";

if(month<10){

mon="0"+month;

}else{

mon=month+"";

}

if(day<10){

d="0"+day;

}else{

d=""+day;

}

string ret=year+""+mon+""+d;

return ret;

}

/**

*

* @title: ftime2

* @description: todo 获取time这个日期以后dayafter天的日期

* @return

*/

public static string ftime2(string time,int dayafter){

date date = null;

try {

date = sdf.parse(time);

} catch (parseexception e) {

throw new runtimeexception(e);

}

calendar calendar = calendar.getinstance();

calendar.settime(date);

if(dayafter>0){

calendar.add(calendar.day_of_month, +dayafter);//后15天数据

date = calendar.gettime();

calendar.settime(date);

}

int year=calendar.get(calendar.year);

int month=calendar.get(calendar.month) + 1;

int day = calendar.get(calendar.day_of_month);

string mon="";

string d="";

if(month<10){

mon="0"+month;

}else{

mon=month+"";

}

if(day<10){

d="0"+day;

}else{

d=""+day;

}

string ret=year+""+mon+""+d;

return ret;

}

/**

*

* @title: getdefaulttime

* @description: todo 获取昨天的日期

* @return

*/

public static string getdefaulttime(){

calendar calendar = calendar.getinstance();

calendar.add(calendar.day_of_month, -1);//前1天

date date = calendar.gettime();

string time=sdf.format(date);

return time;

}

/**

*

* @title: getsunday

* @description: todo 获取最近一个星期天

* @return

*/

public static string getsunday(){

simpledateformat f = new simpledateformat("yyyymmdd");

calendar c = calendar.getinstance();

c.set(calendar.day_of_week, calendar.sunday);

return f.format(c.gettime());

}

/**

*

* @title: getmonthfirstday

* @description: todo 获取本月第一天

* @return

*/

public static string getcurrentmonthfirstday(){

calendar cal_1=calendar.getinstance();//获取当前日期

cal_1.add(calendar.month, 0);

cal_1.set(calendar.day_of_month,1);//设置为1号,当前日期既为本月第一天

string firstday = sdf.format(cal_1.gettime());

return firstday;

}

/**

*

* @title: getmonthfirstday

* @description: todo 获取上月第一天

* @return

*/

public static string getpreviousmonthfirstday(){

//获取当前月第一天:

calendar c = calendar.getinstance();

c.add(calendar.month, -1);

c.set(calendar.day_of_month,1);//设置为1号,当前日期既为本月第一天

string first = sdf.format(c.gettime());

return first;

}

/**

*

* @title: getmonthfirstday

* @description: todo 获取上月最后一天

* @return

*/

public static string getpreviousmonthlastday(){

//获取当前月最后一天

calendar ca = calendar.getinstance();

ca.set(calendar.day_of_month,0);//

string lastday = sdf.format(ca.gettime());

return lastday;

}

/**

*

* @title: getcurrentmonthlastday

* @description: todo 获取指定时间最后一天

* @return

*/

public static string getcurrentmonthlastday(string time){

date date =null;

try {

date= sdf.parse(time);

} catch (parseexception e) {

// todo auto-generated catch block

e.printstacktrace();

}

//获取当前月最后一天

calendar ca = calendar.getinstance();

ca.settime(date);

ca.set(calendar.day_of_month,

ca.getactualmaximum(calendar.day_of_month)); //

string lastday = sdf.format(ca.gettime());

return lastday;

}

/***

*

* @title: getcurrentweekday

* @description: todo 获取本周周一

*/

public static string getcurrentmonday(){

calendar cal = calendar.getinstance();

cal.setfirstdayofweek(calendar.monday);//将每周第一天设为星期一,默认是星期天

cal.add(calendar.date, 0);

cal.set(calendar.day_of_week,calendar.monday);

string monday = sdf.format(cal.gettime());

return monday;

}

/***

*

* @title: getprevioussunday

* @description: todo 获取上周周日

*/

public static string getprevioussunday(){

calendar cal = calendar.getinstance();

cal.setfirstdayofweek(calendar.monday);//将每周第一天设为星期一,默认是星期天

cal.add(calendar.date, -1*7);

cal.set(calendar.day_of_week, calendar.sunday);

string sunday =sdf.format(cal.gettime());

return sunday;

}

/**

*

* @title: getminisencond

* @description: todo 将日期转换为毫秒数

* @param str

* @return

*/

public static string getminisencond(string str){

long millionseconds=0;

try {

millionseconds = sdfymdhm.parse(str).gettime();//毫秒

} catch (parseexception e) {

// todo auto-generated catch block

e.printstacktrace();

}

return millionseconds+"";

}

/**

*

* @title: getdatesencond

* @description: todo 将日期转换为毫秒数

* @param str

* @return

*/

public static long getdatesencond(string str){

long millionseconds=0;

try {

millionseconds = sdfymdhms.parse(str).gettime();//毫秒

} catch (parseexception e) {

// todo auto-generated catch block

e.printstacktrace();

}

return millionseconds;

}

/**

* 计算日期相差天数

* @param str1

* @param str2

* @return

*/

public static int getdistanceoftwodate(string str1,string str2){

int result=0;

try{

date date1 = sdf.parse(str1);

date date2 =sdf.parse(str2);

calendar acalendar = calendar.getinstance();

acalendar.settime(date1);

int day1 = acalendar.get(calendar.day_of_year);

acalendar.settime(date2);

int day2 = acalendar.get(calendar.day_of_year);

result = day1-day2;

}catch(exception e){

e.printstacktrace();

}

return result;

}

/**

*

* @title: long2date

* @description: todo long 转日期(年-月-日 时-分-秒)

* @param timestamp

* @return

*/

public static string longtodate(long msecond){

date date = new date(msecond);

return sdfymdhms.format(date);

}

}

ps:这里再为大家推荐几款关于日期与天数计算的在线工具供大家使用:

在线日期/天数计算器:

在线万年历日历:

在线阴历/阳历转换工具:

希望本文所述对大家java程序设计有所帮助。

希望与广大网友互动??

点此进行留言吧!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值