日期中的特殊加减计算方法

1、某个日期加/减去几个月,得到新的日期

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class Test {

	// 1970中的70的意思。主要用于当传进两位的年份时进行判断。
	public static int YY_PART_YEAR = 70;
	/**
	 * @param args
	 * @throws IOException 
	 * @throws NumberFormatException 
	 */
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("输入年份");  
		int sourceYear = Integer.parseInt(br.readLine());
		System.out.print("输入月份");  
		int sourceMonth = Integer.parseInt(br.readLine());
		System.out.println("输入要加的月份");
		int period = Integer.parseInt(br.readLine());
		
		// 计算总共多少个月
		// 减1是为了避开被12整除和取余的情况,月份中的1月及12月被合并在一起,变成0月。
		int totalMonths = sourceYear*12 + sourceMonth + period - 1;
		int targetYear = (int)totalMonths/12;
		// 在月份取余时把刚才减掉的一个月补回来。
		// 如:2009年12月减1个月,变成2009年11月,取余(为了避开12倍数)后将保留11月,然后再补回来。
		int targetMonth = totalMonths%12 + 1;
		System.out.println(targetYear+"-"+targetMonth);
		
		System.out.println(convert_month_to_period(2010*12));

	}
	
	// 以下两个方法是从Mysql的源码中获取的。
	
	public static long convert_period_to_month(long period)
	{
		long a,b;
		  if (period == 0)
		    return 0L;
		  if ((a=period/100) < YY_PART_YEAR)
		    a+=2000;
		  else if (a < 100)
		    a+=1900;
		  b=period%100;
		  return a*12+b-1;
		}
	public static long convert_month_to_period(long month)
	{
	  long year;
	  if (month == 0L)
	    return 0L;
	  if ((year=month/12) < 100)
	  {
	    year+=(year < YY_PART_YEAR) ? 2000 : 1900;
	  }
	  return year*100+month%12+1;
	}

}

2、转自:http://zhidao.baidu.com/question/63595877

   我要算的是比如说2008年08月减去2007年11月的结果,意思是说某一年和某一年之间的月份差,要怎么计算?

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test21
{

    public static void main(String[] args) throws NumberFormatException,
            IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("输入第一个年份");
        int yearFirst = Integer.parseInt(br.readLine());
        System.out.print("输入第一个月份");
        int monthFirst = Integer.parseInt(br.readLine());
        System.out.println("输入第二个年份");
        int yearSecond = Integer.parseInt(br.readLine());
        System.out.print("输入第二个月份");
        int monthSecond = Integer.parseInt(br.readLine());

        // 计算月份差算法
        int monthsub = (yearFirst - yearSecond) * 12 + (monthFirst - monthSecond);
        System.out.println("相差的月份" + monthsub);
    }
}

3、系统星期转换为中国的星期制

day_in_week = (7 + day_in_week - 1) % 7;
if(day_in_week == 0)
{
       day_in_week = 7;
}

 

4、月份换算归属为哪一个季度

import java.util.Date;

public class Test
{

    public static void main(String argv[])
    {
        Date d = new Date();
        int month = d.getMonth();
        int quarter = (int) (Math.floor(month * 3 / 10) + 1);
        System.out.println(quarter);
    }

}

 

1 2 3 4 5 6 7 8 9 10 11 12
乘3 3 6 9 12 15 18 21 24 27 30 33 36
除10 0.3 0.6 0.9 1.2 1.5 1.8 2.1 2.4 2.7 3.0 3.3 3.6
取整 0 0 0 1 1 1 2 2 2 3 3 3
加1 1 1 1 2 2 2 3 3 3 4 4 4

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值