Calendar获取时间问题。急,在线等待

chenhuan818 2008-04-27 03:20:45
Calendar怎么获得当天的23点59分59秒,
怎么获得当月最后一天的23点59分59秒,
怎么获得当年的最后一天的23点59分59秒

我要做个统计,统计每个月的每一天的pv。我表的设计是day1,day2,。。。day31,MONTH,year。这样设计的。

...全文
674 点赞 收藏 7
写回复
7 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
chenhuan818 2008-04-27
那如何得到上个月的最后一天,上一年的最后一天呢。
回复
joejoe1991 2008-04-27
我那个有问题,.....
顶anqini
回复
anqini 2008-04-27

package zhao;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Son {
/**
* Calendar怎么获得当天的23点59分59秒,
怎么获得当月最后一天的23点59分59秒,
怎么获得当年的最后一天的23点59分59秒
*/
public static void main(String[] args) {
SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Calendar c = Calendar.getInstance();
c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), 23, 59, 59);

System.out.println("当天的23点59分59秒→"+ sm.format(c.getTime()) );
c.set(Calendar.DATE, c.getActualMaximum(Calendar.DATE));
System.out.println("当月最后一天的23点59分59秒→"+ sm.format(c.getTime()) );

c.set(Calendar.MONTH, 11);
c.set(Calendar.DATE, c.getActualMaximum(Calendar.DATE));
System.out.println("当年的最后一天的23点59分59秒 →"+ sm.format(c.getTime()) );
}
}

回复
joejoe1991 2008-04-27
改了一下:

	public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);

System.out.println("当天23:59:59\t" + format.format(cal.getTime()));

cal.set(Calendar.DATE, 0);
cal.add(Calendar.MONTH, 1);

System.out.println("本月最后一天23:59:59\t" + format.format(cal.getTime()));

cal.set(Calendar.MONTH, 0);
cal.set(Calendar.DATE, 0);
cal.add(Calendar.YEAR, 1);
cal.add(Calendar.DATE, -1);

System.out.println("本年最后一天23:59:59\t" + format.format(cal.getTime()));
}


当天23:59:59	2008-04-27 23:59:59
本月最后一天23:59:59 2008-04-30 23:59:59
本年最后一天23:59:59 2008-12-30 23:59:59
回复
joejoe1991 2008-04-27
		Calendar cal = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

cal.set(Calendar.HOUR, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);

System.out.println("当天23:59:59\t" + format.format(cal.getTime()));

cal.set(Calendar.DATE, 0);
cal.add(Calendar.MONTH, 1);

System.out.println("本月最后一天23:59:59\t" + format.format(cal.getTime()));

cal.set(Calendar.MONTH, 0);
cal.set(Calendar.DATE, 0);
cal.add(Calendar.YEAR, 1);
cal.add(Calendar.DATE, -1);

System.out.println("本年最后一天23:59:59\t" + format.format(cal.getTime()));
回复
Steve_Samuelson 2008-04-27
程序运行结果如下:
2008-04-27 23:59:59 星期日
2008-04-30 23:59:59 星期三
2008-12-31 23:59:59 星期三

package $4月27日_星期日;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;


public class $1543_Calendar {
public static void main(String[] args) {
GregorianCalendar cal = new GregorianCalendar();
//设置当天的23时29分29秒
cal.set(Calendar.HOUR_OF_DAY,23);
cal.set(Calendar.MINUTE,59);
cal.set(Calendar.SECOND,59);
System.out.println(new SimpleDateFormat("yyyy-MM-dd kk:mm:ss EEEE").format(cal.getTime()));
//设置当月最后一天的23时29分29秒
GregorianCalendar cal2 = new GregorianCalendar();
int month = cal2.get(Calendar.MONTH)+1;
int year = cal2.get(Calendar.YEAR);
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
cal2.set(Calendar.DAY_OF_MONTH,31);
}else if(month==2){
if((year%4==0&&year%100!=0)||year%400==0){
cal2.set(Calendar.DAY_OF_MONTH,29);
}else{
cal2.set(Calendar.DAY_OF_MONTH,28);
}
}else{
cal2.set(Calendar.DAY_OF_MONTH,30);
}
cal2.set(Calendar.HOUR_OF_DAY,23);
cal2.set(Calendar.MINUTE,59);
cal2.set(Calendar.SECOND,59);
System.out.println(new SimpleDateFormat("yyyy-MM-dd kk:mm:ss EEEE").format(cal2.getTime()));
//设置当年最后一天的23时29分29秒
GregorianCalendar cal3 = new GregorianCalendar();
cal3.set(Calendar.MONTH, 11);
cal3.set(Calendar.DAY_OF_MONTH,31);
cal3.set(Calendar.HOUR_OF_DAY,23);
cal3.set(Calendar.MINUTE,59);
cal3.set(Calendar.SECOND,59);
System.out.println(new SimpleDateFormat("yyyy-MM-dd kk:mm:ss EEEE").format(cal3.getTime()));
}
}
回复
Steve_Samuelson 2008-04-27
这样符合你的需求吗?
程序运行结果如下:
2008-04-27 23:59:59 星期日
2008-04-30 23:59:59 星期三
2008-12-31 23:59:59 星期三

package $4月27日_星期日;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;


public class $1543_Calendar {
public static void main(String[] args) {
GregorianCalendar cal = new GregorianCalendar();
//设置当天的23时29分29秒
cal.set(Calendar.HOUR_OF_DAY,23);
cal.set(Calendar.MINUTE,59);
cal.set(Calendar.SECOND,59);
System.out.println(new SimpleDateFormat("yyyy-MM-dd kk:mm:ss EEEE").format(cal.getTime()));
//设置当月最后一天的23时29分29秒
GregorianCalendar cal2 = new GregorianCalendar();
int month = cal2.get(Calendar.MONTH)+1;
int year = cal2.get(Calendar.YEAR);
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
cal2.set(Calendar.DAY_OF_MONTH,31);
}else if(month==2){
if((year%4==0&&year%100!=0)||year%400==0){
cal2.set(Calendar.DAY_OF_MONTH,29);
}else{
cal2.set(Calendar.DAY_OF_MONTH,28);
}
}else{
cal2.set(Calendar.DAY_OF_MONTH,30);
}
cal2.set(Calendar.HOUR_OF_DAY,23);
cal2.set(Calendar.MINUTE,59);
cal2.set(Calendar.SECOND,59);
System.out.println(new SimpleDateFormat("yyyy-MM-dd kk:mm:ss EEEE").format(cal2.getTime()));
//设置当年最后一天的23时29分29秒
GregorianCalendar cal3 = new GregorianCalendar();
cal3.set(Calendar.MONTH, 11);
cal3.set(Calendar.DAY_OF_MONTH,31);
cal3.set(Calendar.HOUR_OF_DAY,23);
cal3.set(Calendar.MINUTE,59);
cal3.set(Calendar.SECOND,59);
System.out.println(new SimpleDateFormat("yyyy-MM-dd kk:mm:ss EEEE").format(cal3.getTime()));
}
}
回复
相关推荐
发帖
Java SE
创建于2007-09-28

6.1w+

社区成员

Java 2 Standard Edition
申请成为版主
帖子事件
创建了帖子
2008-04-27 03:20
社区公告
暂无公告