Date

一:时间格式转换

1.String转成Date:String的格式需要与SimpleDateFormat构造器里所定义的格式必须一致,不然将不能转换

2.Date转成String: 一个日期可以根据需要转成自己需要的任意格式的String ,只需要在SimpleDateFormat构造器定义格式

/*
* Copyright @ 2015 UWITEC.COM
* All right reserved.
*/
package com.uwitec.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import oracle.sql.TIMESTAMP;

public class DateUtil {
private final static SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");

private final static SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd");

private final static SimpleDateFormat sdfDays = new SimpleDateFormat("yyyyMMdd");

private final static SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

/**
* 获取YYYY格式
*
* @return
*/
public static String getYear() {
return sdfYear.format(new Date());
}

/**
* 获取YYYY-MM-DD格式
*
* @return
*/
public static String getDay() {
return sdfDay.format(new Date());
}

/**
* 获取YYYYMMDD格式
*
* @return
*/
public static String getDays() {
return sdfDays.format(new Date());
}

/**
* 获取YYYY-MM-DD HH:mm:ss格式
*
* @return
*/
public static String getTime() {
return sdfTime.format(new Date());
}

/**
* @Title: compareDate
* @Description: TODO(日期比较,如果s>=e 返回true 否则返回false)
* @param s
* @param e
* @return boolean
* @throws
* @author UWITEC
*/
public static boolean compareDate(String s, String e) {
if (fomatDate(s) == null || fomatDate(e) == null) {
return false;
}
return fomatDate(s).getTime() >= fomatDate(e).getTime();
}

/**
* 将字符串格式化为日期,字符串类型 "yyyy-MM-dd"
*
* @return
*/
public static Date fomatDate(String date) {
DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
try {
if(Tools.notEmpty(date)){
return fmt.parse(date);
}
return null;
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
/**
* 将字符串格式化为日期,字符串类型 "yyyy-MM-dd HH:mm:ss"
*
* @return
*/
public static String fomatDateS(TIMESTAMP timestamp) {

DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date d = null;
if(timestamp != null)
{
d=timestamp.dateValue();
return fmt.format(d).toString();
}
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String fomatDateSs(Timestamp timestamp) {

DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
return fmt.format(timestamp).toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static Date formatStr(String str)
{
DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
return fmt.parse(str);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
public static Date formatD(Date date)
{
DateFormat fmt=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

try{
return formatStr(fmt.format(date));
}
catch(Exception e)
{
e.printStackTrace();
return null;
}

}

/**
* 校验日期是否合法
*
* @return
*/
public static boolean isValidDate(String s) {
DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
try {
fmt.parse(s);
return true;
} catch (Exception e) {
// 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
return false;
}
}

public static int getDiffYear(String startTime, String endTime) {
DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
try {
int years = (int) (((fmt.parse(endTime).getTime() - fmt.parse(startTime).getTime()) / (1000 * 60 * 60 * 24)) / 365);
return years;
} catch (Exception e) {
// 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
return 0;
}
}

/**
* <li>功能描述:时间相减得到天数
*
* @param beginDateStr
* @param endDateStr
* @return long
* @author UWITEC
*/
public static long getDaySub(String beginDateStr, String endDateStr) {
long day = 0;
java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
java.util.Date beginDate = null;
java.util.Date endDate = null;

try {
beginDate = format.parse(beginDateStr);
endDate = format.parse(endDateStr);
} catch (ParseException e) {
e.printStackTrace();
}
day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
// System.out.println("相隔的天数="+day);

return day;
}

/**
* 得到n天之后的日期
*
* @param days
* @return
*/
public static String getAfterDayDate(String days) {
int daysInt = Integer.parseInt(days);

Calendar canlendar = Calendar.getInstance(); // java.util包
canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
Date date = canlendar.getTime();

SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr = sdfd.format(date);

return dateStr;
}

/**
* 得到n天之后是周几
*
* @param days
* @return
*/
public static String getAfterDayWeek(String days) {
int daysInt = Integer.parseInt(days);

Calendar canlendar = Calendar.getInstance(); // java.util包
canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
Date date = canlendar.getTime();

SimpleDateFormat sdf = new SimpleDateFormat("E");
String dateStr = sdf.format(date);

return dateStr;
}

/**
* 格式化日期为字符串 字符输出串格式: "yyyy-MM-dd HH:mm:ss"
* @param date
* @return
*/
public static String formatDate(Date date){
if(null != date){
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return dateFormat.format(date);
}
return "";
}

/**
* 日期加一天
* @param date
* @return
*/
public static Date dateAddOneDay(Date date){
if(null != date){
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动
Date newDate=calendar.getTime();
return newDate;
}else{
return null;
}
}

/**
* 当前日期前六天
* @param date
* @return
*/
public static Date dateTDay(){
Date date=new Date();
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.add(calendar.DATE,-6);//把日期往后增加一天.整数往后推,负数往前移动
Date newDate=calendar.getTime();
return newDate;
}

/**
* 获取当天时间的00:00:00
* @return
*/
public static Date todayTime(){
Date date=new Date();
DateFormat fmt=new SimpleDateFormat("yyyy-MM-dd 00:00:00");
try{
return formatStr(fmt.format(date));
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}


/**
* 获取当月第一天
* @return
*/
public static Date getMouthFirstDay(){
DateFormat format=new SimpleDateFormat("yyyy-MM-dd");
Date date = fomatDate("2016-06-01");
try{
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, 0);
c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
String first = format.format(c.getTime());
System.out.println("===============first:"+first);
return fomatDate(first);
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}


/**
* 获取当月最后一天
* @return
*/
public static Date getMouthLastDay(){
DateFormat format=new SimpleDateFormat("yyyy-MM-dd");
try{
Calendar ca = Calendar.getInstance();
ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
String last = format.format(ca.getTime());
System.out.println("===============last:"+last);
return fomatDate(last);
}
catch(Exception e)
{
e.printStackTrace();
return null;
}

}
/**
* 获取某月第一天
* @return
*/
public static Date getRandomMouthFirstDay(String date){
DateFormat format=new SimpleDateFormat("yyyy-MM-dd");
try{
String year = date.substring(0, 4);
int years = Integer.parseInt(year);
String mouth1 = date.substring(5, 7);
String mouth2 = mouth1.replace("0", "");
int mouth = Integer.parseInt(mouth2);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR,years);
cal.set(Calendar.MONTH, mouth);
cal.set(Calendar.DAY_OF_MONTH, 1);//去掉是后一个月第一天
cal.add(Calendar.DAY_OF_MONTH, -1);//去掉是后一个月第一天
cal.set(Calendar.DAY_OF_MONTH, 1);//去掉是某一个月最后一天
String firstDate = format.format(cal.getTime());
System.out.println(firstDate);
return fomatDate(firstDate);
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}

/**
* 获取某月最后一天
* @return
*/
public static Date getRandomMouthLastDay(String date){
DateFormat format=new SimpleDateFormat("yyyy-MM-dd");
try{
String year = date.substring(0, 4);
int years = Integer.parseInt(year);
String mouth1 = date.substring(5, 7);
String mouth2 = mouth1.replace("0", "");
int mouth = Integer.parseInt(mouth2);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR,years);
cal.set(Calendar.MONTH, mouth);
cal.set(Calendar.DAY_OF_MONTH, 1);//去掉是后一个月最后一天
cal.add(Calendar.DAY_OF_MONTH, -1);//去掉是后一个月第一天
String lastDate = format.format(cal.getTime());
System.out.println(lastDate);
return fomatDate(lastDate);
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}


public static void main(String[] args) {
//System.out.println(todayTime().toString());
/*getMouthFirstDay();
getMouthLastDay();*/

System.out.println(getRandomMouthFirstDay("2016-03-06"));
System.out.println(getRandomMouthLastDay("2016-09-04"));

}
}

原文地址:https://www.cnblogs.com/jianyi12/p/5631013.html