class="java" name="code">package com.dc.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/* ****************** 类说明 *********************
* class : TimeUtil
* @author : ncc
* create time : 2017-12-19 上午11:03:11
* @version : 1.0
* description : 日期时间获取
* @see :
* ************************************************/
public class TimeUtil {
/* ********************************************
* method name : date
* description : 按指定格式获取当前时间
* @return : String
* @param : @param fmt
* @param : @return
* modified : ncc , 2017-12-19
* @see :
* ********************************************/
public static String date(String fmt) {
return new SimpleDateFormat(fmt).format(new Date());
}
/* ********************************************
* method name : date
* description : 获取指定时间对应的指定格式获取
* @return : String
* @param : @param fmt
* @param : @param t
* @param : @return
* modified : ncc , 2017-12-19
* @see :
* ********************************************/
public static String date(String fmt, long t) {
return new SimpleDateFormat(fmt).format(new Date(t));
}
/* ********************************************
* method name : date8
* description : 获取八位日期 yyyyMMdd
* @return : String
* @param : @return
* modified : ncc , 2017-12-19
* @see :
* ********************************************/
public static String date8() {
return new SimpleDateFormat("yyyyMMdd").format(new Date());
}
/* ********************************************
* method name : date8
* description : 将时间转换为8位日期 yyyyMMdd
* @return : String
* @param : @param date
* @param : @return
* modified : ncc , 2017-12-19
* @see :
* ********************************************/
public static String date8(Date date) {
return new SimpleDateFormat("yyyyMMdd").format(date);
}
/* ********************************************
* method name : time6
* description : 获取6位当前时间 HHmmss
* @return : String
* @param : @return
* modified : ncc , 2017-12-19
* @see :
* ********************************************/
public static String time6() {
return new SimpleDateFormat("HHmmss").format(new Date());
}
/* ********************************************
* method name : time9
* description : 获取9位当前时间 HHmmssSSS
* @return : String
* @param : @return
* modified : ncc , 2017-12-19
* @see :
* ********************************************/
public static String time9() {
return new SimpleDateFormat("HHmmssSSS").format(new Date());
}
/* ********************************************
* method name : time6
* description : 获取指定时间对应的6位时间 HHmmss
* @return : String
* @param : @param date
* @param : @return
* modified : ncc , 2017-12-19
* @see :
* ********************************************/
public static String time6(Date date) {
return new SimpleDateFormat("HHmmss").format(date);
}
/* ********************************************
* method name : datetime12
* description : 获取指定时间对应的12位时间 yyyyMMddHHmm
* @return : String
* @param : @param date
* @param : @return
* modified : ncc , 2017-12-19
* @see :
* ********************************************/
public static String datetime12(Date date) {
return new SimpleDateFormat("yyyyMMddHHmm").format(date);
}
/* ********************************************
* method name : datetime14
* description : 获取指定时间对应的14位时间 yyyyMMddHHmmss
* @return : String
* @param : @return
* modified : ncc , 2017-12-19
* @see :
* ********************************************/
public static String datetime14( ) {
return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
}
/* ********************************************
* method name : calcMinute
* description : 获取指定时间指定分钟之后的时间
* @return : String yyyyMMddHHmm
* @param : @param time12 yyyyMMddHHmm
* @param : @param s 指定分钟,可为负
* @param : @return
* modified : ncc , 2017-12-19
* @see :
* ********************************************/
public static String calcMinute(String time12, int s) {
Calendar cal = Calendar.getInstance();
cal.set(Integer.parseInt(time12.substring(0, 4)), Integer
.parseInt(time12.substring(4, 6)) - 1, Integer.parseInt(time12
.substring(6, 8)), Integer.parseInt(time12.substring(8, 10)),
Integer.parseInt(time12.substring(10, 12)));
cal.add(Calendar.MINUTE, s);
return new SimpleDateFormat("yyyyMMddHHmm").format(cal.getTime());
}
/* ********************************************
* method name : getYesterday
* description : 获取指定时间前一天的8位日期 yyyyMMdd
* @return : String
* @param : @param today
* @param : @return
* modified : ncc , 2017-12-19
* @see :
* ********************************************/
public static String getYesterday(Date today){
Calendar cal = Calendar.getInstance();
cal.setTime(today);
cal.add(Calendar.DAY_OF_YEAR, -1);
return new SimpleDateFormat("yyyyMMdd").format(cal.getTime());
}
/**
* <br>description : 获取当前日期的前几天日期
* @param date8
* @param i
* @return
* @author xiatian
* @version 1.0
* @date Oct 21, 201310:03:05 AM
*/
/* ********************************************
* method name : calcDay
* description : 获取指定日期yyyyMMdd的前几天的8位日期yyyyMMdd
* @return : String
* @param : @param date8 指定日期
* @param : @param i 前n天
* @param : @return
* modified : ncc , 2017-12-19
* @see :
* ********************************************/
public static String calcDay(String date8,int i){
Calendar cal = Calendar.getInstance();
cal.set(Integer.parseInt(date8.substring(0,4)),Integer.parseInt(date8.substring(4,6))-1,
Integer.parseInt(date8.substring(6)));
cal.add(Calendar.DAY_OF_YEAR, i);
return new SimpleDateFormat("yyyyMMdd").format(cal.getTime());
}
/* ********************************************
* method name : getMonthLastDay
* description : 获取本月最后一天的8位日期 yyyyMMdd
* @return : String
* @param : @return
* modified : ncc , 2017-12-19
* @see :
* ********************************************/
public static String getMonthLastDay() {
Calendar lastDate = Calendar.getInstance();
lastDate.set(Calendar.DATE, 1);// 设为当前月的1号
lastDate.add(Calendar.MONTH, 1);// 加一个月,变为下月的1号
lastDate.add(Calendar.DATE, -1);// 减去一天,变为当月最后一天
return new SimpleDateFormat("yyyyMMdd").format(lastDate.getTime());
}
/* ********************************************
* method name : getNextMonth
* description : 得到下一个月的8位日期yyyyMMdd
* @return : String
* @param : @return
* modified : ncc , 2017-12-19
* @see :
* ********************************************/
public static String getNextMonth() {
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, 1);// 加一个月
return new SimpleDateFormat("yyyyMMdd").format(c.getTime());
}
/* ********************************************
* method name : getNextMonth
* description : 得到指定日期下一个月的8位日期yyyyMMdd,若没有,则往前推,知道有为止,如20180130,返回20180228
* @return : String
* @param : @return
* modified : ncc , 2017-12-19
* @see :
* ********************************************/
public static String getNextMonth(String date8){
Calendar cal = Calendar.getInstance();
cal.set(Integer.parseInt(date8.substring(0,4)),Integer.parseInt(date8.substring(4,6))-1,
Integer.parseInt(date8.substring(6)));
cal.add(Calendar.MONTH, 1);// 加一个月
return new SimpleDateFormat("yyyyMMdd").format(cal.getTime());
}
/* ********************************************
* method name : getPreviousMonthEnd
* description : 获取上月最后一天 yyyyMMdd
* @return : String
* @param : @return
* modified : ncc , 2017-12-19
* @see :
* ********************************************/
public static String getPreviousMonthEnd() {
Calendar lastDate = Calendar.getInstance();
lastDate.add(Calendar.MONTH, -1);// 减一个月
lastDate.set(Calendar.DATE, 1);// 把日期设置为当月第一天
lastDate.roll(Calendar.DATE, -1);// 日期回滚一天,也就是本月最后一天
return new SimpleDateFormat("yyyyMMdd").format(lastDate.getTime());
}
/* ********************************************
* method name : getTimestamp
* description : 将时间转化成时间戳
* @return : String
* @param : @param string
* @param : @return
* modified : ncc , 2017-12-19
* @see :
* ********************************************/
public static String getTimestamp(String time14) {
Date date = null;
try {
date = new SimpleDateFormat("yyyyMMddHHmmss").parse(time14);
} catch (ParseException e) {
e.printStackTrace();
}
if (date == null) {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
}else {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
}
}
/* ********************************************
* method name : getWhatDayByDate
* description : 获取指定日期为星期几
* @return : String
* @param : @param date
* @param : @return
* @param : @throws Exception
* modified : ncc , 2017-12-19
* @see :
* ********************************************/
public static String getWhatDayByDate(String date) throws Exception {
Calendar calendar = Calendar.getInstance();
Date temp = new SimpleDateFormat("yyyyMMdd").parse(date);
calendar.setTime(temp);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
// System.out.println(date + ":" + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println(date + ":" + calendar.get(Calendar.WEEK_OF_MONTH));
System.out.println(date + ":" + calendar.get(Calendar.WEEK_OF_YEAR));
return dayOfWeek + "";
}
/* ********************************************
* method name : getWeekByDate
* description : 获取指定日期yyyyMMdd为当年第几周
* @return : String
* @param : @param date
* @param : @return
* @param : @throws Exception
* modified : ncc , 2017-12-19
* @see :
* ********************************************/
public static String getWeekByDate(String date8) throws Exception {
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
Date temp = null;
try {
temp = new SimpleDateFormat("yyyyMMdd").parse(date8);
} catch (ParseException e) {
e.printStackTrace();
}
if (temp == null) {
temp = new Date();
}
calendar.setTime(temp);
StringBuffer result = new StringBuffer();
int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
int year = calendar.get(Calendar.YEAR);
if (weekOfYear == 1 && calendar.get(Calendar.MONTH) == 11) {
year = year + 1;
}
result.append(year);
if (weekOfYear < 10) {
result.append("0");
}
result.append(weekOfYear);
// System.out.println(date + ",month:" + calendar.get(Calendar.MONTH) + ",week:" + result + ",day:" + (calendar.get(Calendar.DAY_OF_WEEK)-1));
return result.toString();
}
public static void main(String[] args) throws ParseException{
System.out.println(getNextMonth("20180130"));
}
}
?
?