标签:不能 char 情况 max gen 大于 lda eth between
计算某一月份的最大天数
- Calendar time=Calendar.getInstance();
- int day=time.getActualMaximum(Calendar.DAY_OF_MONTH);
注:在使用set方法之前,必须先clear一下,否则很多信息会继承自系统当前时间
2.Calendar和Date的转化
(1) Calendar转化为Date
- Calendar cal=Calendar.getInstance();
- Date date=cal.getTime();
(2) Date转化为Calendar
- Date date=new Date();
- Calendar cal=Calendar.getInstance();
- cal.setTime(date);
3.格式化输出日期时间
- import java.util.Date;
- Date date=new Date();
- SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
- System.out.println(df.format(date));
4.计算一年中的第几星期
(1)计算某一天是一年中的第几星期
- Calendar cal=Calendar.getInstance();
- cal.set(Calendar.YEAR, 2006);
- cal.set(Calendar.MONTH, 8);
- cal.set(Calendar.DAY_OF_MONTH, 3);
- int weekno=cal.get(Calendar.WEEK_OF_YEAR);
(2)计算一年中的第几星期是几号
- SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
- Calendar cal=Calendar.getInstance();
- cal.set(Calendar.YEAR, 2006);
- cal.set(Calendar.WEEK_OF_YEAR, 1);
- cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
- System.out.println(df.format(cal.getTime()));
输出:
2006-01-02
5.add()和roll()的用法
(1)add()方法
- SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
- Calendar cal=Calendar.getInstance();
- cal.set(Calendar.YEAR, 2006);
- cal.set(Calendar.MONTH, 8);
- cal.set(Calendar.DAY_OF_MONTH, 3);
- cal.add(Calendar.DATE, -4);
- Date date=cal.getTime();
- System.out.println(df.format(date));
- cal.add(Calendar.DATE, 4);
- date=cal.getTime();
- System.out.println(df.format(date));
输出:
2006-08-30
2006-09-03
(2)roll方法
- cal.set(Calendar.YEAR, 2006);
- cal.set(Calendar.MONTH, 8);
- cal.set(Calendar.DAY_OF_MONTH, 3);
- cal.roll(Calendar.DATE, -4);
- date=cal.getTime();
- System.out.println(df.format(date));
- cal.roll(Calendar.DATE, 4);
- date=cal.getTime();
- System.out.println(df.format(date));
输出:
2006-09-29
2006-09-03
可见,roll()方法在本月内循环,一般使用add()方法;
6.计算两个任意时间中间的间隔天数
(1)传进Calendar对象
- public int getIntervalDays(Calendar startday,Calendar endday)...{
-
- if(startday.after(endday))...{
- Calendar cal=startday;
- startday=endday;
- endday=cal;
- }
-
- long sl=startday.getTimeInMillis();
- long el=endday.getTimeInMillis();
-
- long ei=el-sl;
-
- return (int)(ei/(1000*60*60*24)); }
(2)传进Date对象
- public int getIntervalDays(Date startday,Date endday)...{
-
- if(startday.after(endday))...{
- Date cal=startday;
- startday=endday;
- endday=cal;
- }
-
- long sl=startday.getTime();
- long el=endday.getTime();
-
- long ei=el-sl;
-
- return (int)(ei/(1000*60*60*24));
- }
同理,可以用相同的方法计算出任意两个时间相隔的小时数,分钟数,秒钟数等
注:以上方法是完全按时间计算,有时并不能令人满意,如:
startday="2006-10-11 20:00:00"
endday="2006-10-12 8:00:00"
- String s1 = "2003-12-12 11:30:24";
- String s2 = "2004-04-01 13:31:40";
- int res = s1.compareTo(s2);
-
-
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Date begin=df.parse("2004-01-02 11:30:24");
- Date end = df.parse("2004-03-26 13:31:40");
- long between=(end.getTime()-begin.getTime())/1000;
- int day=between/(24*3600);
- int hour=between%(24*3600)/3600;
- int minute=between%3600/60;
- int second=between%60/60;
计算结果为0,但是我们也许相让计算结果变为1,此时可以用如下方法实现:
在传参之前,先设定endday的时间,如:
endday.set(Calendar.HOUR_OF_DAY, 23);
endday.set(Calendar.MINUTE, 59);
endday.set(Calendar.SECOND, 59);
endday.set(Calendar.MILLISECOND, 59);
这样再传进去startday,endday,则结果就如我们所愿了。不过,如果嫌以上方法麻烦,可以参考以下方法:
(3)改进精确计算相隔天数的方法
- public int getDaysBetween (Calendar d1, Calendar d2) ...{
- if (d1.after(d2)) ...{
- java.util.Calendar swap = d1;
- d1 = d2;
- d2 = swap;
- }
- int days = d2.get(Calendar.DAY_OF_YEAR) - d1.get(Calendar.DAY_OF_YEAR);
- int y2 = d2.get(Calendar.YEAR);
- if (d1.get(Calendar.YEAR) != y2) ...{
- d1 = (Calendar) d1.clone();
- do ...{
- days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);
- d1.add(Calendar.YEAR, 1);
- } while (d1.get(Calendar.YEAR) != y2);
- }
- return days;
- }
得到明天的算法
- GregorianCalendar gc = new GregorianCalendar();
- gc.add(GregorianCalendar.DATE,1);
- SimpleDateFormat ymdf = new SimpleDateFormat( "yyyy-MM-dd ");
- System.out.println(ymdf.format(new Date(gc.getTimeInMillis())));
- public String subDate(Date startday,int time){
- long Time1=startday.getTime()/1000-60*time;
- startday.setTime(Time1*1000);
- return CommonFunc.ConvertDateToStr(startday,Constants.YYYYMMDDHHMMSS);
- }
【代码实现】:两个日期之间的工作日时间差(精确到毫秒)
【代码特点】:
1.支持跨年。
2.获取的是精确到毫秒的时间差。
3.国家法定假日未计算在内(像阴历这种的只有通过加入LIST的方法了=。=)。
- package com.wednesday.tools.timetools;
-
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
-
- public class GetWorkDayTimeMillisecond {
-
-
- public static void main(String[] args) {
-
- GetWorkDayTimeMillisecond a = new GetWorkDayTimeMillisecond();
- Long b=a.getWorkdayTimeInMillis("2010-05-17 8-00-00", "2010-05-18 9-00-00", "yyyy-MM-dd HH-mm-ss");
- System.out.println(b);
- }
-
- public Long getWorkdayTimeInMillis(long start, long end){
-
- if(start>end){
- long temp = start;
- start = end;
- end = temp;
- }
-
- Calendar sdate = Calendar.getInstance();
- Calendar edate = Calendar.getInstance();
- sdate.setTimeInMillis(start);
- edate.setTimeInMillis(end);
-
- if(sdate.get(Calendar.YEAR)==edate.get(Calendar.YEAR)
- && sdate.get(Calendar.WEEK_OF_YEAR)==edate.get(Calendar.WEEK_OF_YEAR)
- && sdate.get(Calendar.DAY_OF_WEEK)!=1 && sdate.get(Calendar.DAY_OF_WEEK)!=7
- && edate.get(Calendar.DAY_OF_WEEK)!=1 && edate.get(Calendar.DAY_OF_WEEK)!=7){
- return new Long(end-start);
- }
-
- Calendar snextM = getNextMonday(sdate);
- Calendar enextM = getNextMonday(edate);
-
- int days = getDaysBetween(snextM, enextM);
-
- int workdays = days/7*5;
-
- long scharge = 0;
- if(sdate.get(Calendar.DAY_OF_WEEK)!=1 && sdate.get(Calendar.DAY_OF_WEEK)!=7){
-
- scharge += (7-sdate.get(Calendar.DAY_OF_WEEK))*24*3600000;
- scharge -= sdate.get(Calendar.HOUR_OF_DAY)*3600000;
- scharge -= sdate.get(Calendar.MINUTE)*60000;
- scharge -= sdate.get(Calendar.SECOND)*1000;
- scharge -= sdate.get(Calendar.MILLISECOND);
- }
-
- long echarge = 0;
- if(edate.get(Calendar.DAY_OF_WEEK)!=1 && edate.get(Calendar.DAY_OF_WEEK)!=7){
-
- echarge += (7-edate.get(Calendar.DAY_OF_WEEK))*24*3600000;
- echarge -= edate.get(Calendar.HOUR_OF_DAY)*3600000;
- echarge -= edate.get(Calendar.MINUTE)*60000;
- echarge -= edate.get(Calendar.SECOND)*1000;
- echarge -= edate.get(Calendar.MILLISECOND);
- }
-
- return workdays*24*3600000+scharge-echarge;
- }
- public Long getWorkdayTimeInMillis(Long start, Long end){
- return getWorkdayTimeInMillis(start.longValue(), end.longValue());
- }
- public Long getWorkdayTimeInMillis(Date start, Date end){
- return getWorkdayTimeInMillis(start.getTime(), end.getTime());
- }
- public Long getWorkdayTimeInMillis(String start, String end, String format){
- SimpleDateFormat sdf = new SimpleDateFormat(format);
- Date sdate;
- Date edate;
- try {
- sdate = sdf.parse(start);
- edate = sdf.parse(end);
- return getWorkdayTimeInMillis(sdate, edate);
- } catch (ParseException e) {
- e.printStackTrace();
- return new Long(0);
- }
- }
- private Calendar getNextMonday(Calendar cal){
- int addnum=9-cal.get(Calendar.DAY_OF_WEEK);
- if(addnum==8)addnum=1;
- cal.add(Calendar.DATE, addnum);
- return cal;
- }
-
- public int getDaysBetween(Calendar start, Calendar end){
- if(start.after(end)){
- Calendar swap = start;
- start = end;
- end = swap;
- }
- int days = end.get(Calendar.DAY_OF_YEAR)- start.get(Calendar.DAY_OF_YEAR);
- int y2 = end.get(Calendar.YEAR);
- if (start.get(Calendar.YEAR) != y2) {
- start = (Calendar) start.clone();
- do {
- days += start.getActualMaximum(Calendar.DAY_OF_YEAR);
- start.add(Calendar.YEAR, 1);
- }while(start.get(Calendar.YEAR) != y2);
- }
- return days;
- }
-
- }
java 常用日期函数总结
标签:不能 char 情况 max gen 大于 lda eth between
原文地址:http://www.cnblogs.com/mfc-itblog/p/6551116.html