package com.sinosig.app.indexcal.utils;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class TimeUtils {
/**
* 得到当前系统日期 author: YT
*
* @return 当前日期的格式字符串,日期格式为"yyyy-MM-dd"
*/
public static String getCurrentDate() {
String pattern = "yyyy-MM-dd";
SimpleDateFormat df = new SimpleDateFormat(pattern);
Date today = new Date();
String tString = df.format(today);
return tString;
}
/**
* 得到当前系统时间 author: YT
*
* @return 当前时间的格式字符串,时间格式为"HH:mm:ss"
*/
public static String getCurrentTime() {
String pattern = "HH:mm:ss";
SimpleDateFormat df = new SimpleDateFormat(pattern);
Date today = new Date();
String tString = df.format(today);
return tString;
}
/**
*
* <p>
* Title: monthsBetween
* </p>
* <p>
* Description: 计算两个时间之间间隔月份
* </p>
*
* @param date1
* @param date2
* @return
* @throws Exception
* @author shijingshuo-ghq 2016年3月14日 上午11:25:18
*/
public static int monthsBetween(Date date1, Date date2) throws Exception {
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(date1);
c2.setTime(date2);
return (c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR)) * 12
+ (c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH));
}
/**
*
* <p>Title: addMonths</p>
* <p>Description: 月份加减</p>
* @param yearMonth
* @return
* @throws Exception
* @author shijingshuo-ghq 2017年3月24日 上午12:52:44
*/
public static String addMonths(String yearMonth,int months) throws Exception{
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
Date date = sdf.parse(yearMonth);
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.MONTH, months);
return sdf.format(c.getTime());
}
}