admin 管理员组

文章数量: 1086019


2024年3月13日发(作者:东莞企业网站建设制作)

如何获取本周,本月,本日的开

始时间和结束时间的时间戳

获取本日,本周,本月的开始时间戳和结束时间戳

这是我最近尝试写的一个程序,可能有许多不足之处,还请见谅。

这是一个类,类里面主要有两个方法。

一个是timeToTimestamp (),一个是getTime()。

前者负责将时间转化成时间戳,后者则负责判断filter值,然后设置相应的date时

间。

注:filter=1 代表本日,filter=2 代表本周,filter=3代表本月。

class TimestampUtils{

private static long startTime = -1;

private static long endTime = -1;

public static void getTime (int filter){

Calendar cal =tance();

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

//设置星期一为一周的开始日

stDayOfWeek();

switch(filter){

case 1://今天

startTime = timeToTimestamp((new Date())+" 00:00:

00");

endTime = timeToTimestamp((new Date())+" 23:59:

59");

break;

case 2://本周

(_OF_WEEK, );

startTime = timeToTimestamp((e())+" 00:0

0:00");

(_OF_WEEK, );

endTime = timeToTimestamp((e())+" 23:5

9:59");

break;

case 3://本月

(, ualMinimum());

startTime = timeToTimestamp((e())+" 00:0

0:00");

(, ualMaximum())

;

endTime = timeToTimestamp((e())+" 23:5

9:59");

break;

default:

break;

}

}

//将时间转换成时间戳

public static long timeToTimestamp (String dateTime){

SimpleDateFormat format2 = new SimpleDateFormat( "yyyy-MM-d

d HH:mm:ss" );

long time = -1;

try {

time = (dateTime).getTime();

} catch (ParseException e) {

printStackTrace();

}

return time;

}

}


本文标签: 时间 程序 网站 企业 东莞