一:時間格式轉換java
1.String轉成Date:String的格式須要與SimpleDateFormat構造器裏所定義的格式必須一致,否則將不能轉換sql
2.Date轉成String: 一個日期能夠根據須要轉成本身須要的任意格式的String ,只須要在SimpleDateFormat構造器定義格式oracle
/*
* Copyright @ 2015 UWITEC.COM
* All right reserved.
*/
package com.uwitec.util;dom
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;orm
import oracle.sql.TIMESTAMP;blog
public class DateUtil {
private final static SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");ip
private final static SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd");字符串
private final static SimpleDateFormat sdfDays = new SimpleDateFormat("yyyyMMdd");get
private final static SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");string
/**
* 獲取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"));
}
}