DateUtil工具類的用法
阿新 • • 發佈:2019-01-22
package com.bjkj.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateUtil {
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
private static SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/*****
* 預設格式為:yyyy-MM-dd
*
* @param date_str
* @return Date
*/
public static Date getDate(String date_str) {
Date date = null;
try {
if(null != date_str && !"".equals(date_str.trim())){
date = sdf.parse(date_str);
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return date;
}
/*****
* 預設格式為:yyyy-MM-dd
*
* @param date_str
* @return String
*/
public static String getDateStr(Date date){
String str = "";
try {
if(null != date){
str = sdf.format(date);
}
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
/*****
* 預設格式為:yyyy-MM-dd HH:mm:ss
*
* @param date_str
* @return Date
*/
public static Date getDate1(String date_str) {
Date date = null;
try {
date = sdf1.parse(date_str);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return date;
}
/*****
* 預設格式為:yyyy-MM-dd HH:mm:ss
*
* @param date_str
* @return String
*/
public static String getDateStr1(Date date){
String str = "";
try {
if(null != date){
str = sdf1.format(date);
}
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
/*****
* 自定義格式進行日期轉換
* @param date_str
* @param format
* @return Date
*/
public static Date getDefaultDate(String date_str, String format) {
Date date = null;
try {
if(null != date_str && !"".equals(date_str)){
date = new SimpleDateFormat(format).parse(date_str);
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return date;
}
/*****
* 自定義格式進行日期轉換
*
* @param date_str
* @return String
*/
public static String getDefaultDateStr(Date date, String format){
String str = "";
try {
if(null != date){
str = new SimpleDateFormat(format).format(date);
}
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateUtil {
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
private static SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/*****
* 預設格式為:yyyy-MM-dd
*
* @param date_str
* @return Date
*/
public static Date getDate(String date_str) {
Date date = null;
try {
if(null != date_str && !"".equals(date_str.trim())){
date = sdf.parse(date_str);
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return date;
}
/*****
* 預設格式為:yyyy-MM-dd
*
* @param date_str
* @return String
*/
public static String getDateStr(Date date){
String str = "";
try {
if(null != date){
str = sdf.format(date);
}
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
/*****
* 預設格式為:yyyy-MM-dd HH:mm:ss
*
* @param date_str
* @return Date
*/
public static Date getDate1(String date_str) {
Date date = null;
try {
date = sdf1.parse(date_str);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return date;
}
/*****
* 預設格式為:yyyy-MM-dd HH:mm:ss
*
* @param date_str
* @return String
*/
public static String getDateStr1(Date date){
String str = "";
try {
if(null != date){
str = sdf1.format(date);
}
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
/*****
* 自定義格式進行日期轉換
* @param date_str
* @param format
* @return Date
*/
public static Date getDefaultDate(String date_str, String format) {
Date date = null;
try {
if(null != date_str && !"".equals(date_str)){
date = new SimpleDateFormat(format).parse(date_str);
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return date;
}
/*****
* 自定義格式進行日期轉換
*
* @param date_str
* @return String
*/
public static String getDefaultDateStr(Date date, String format){
String str = "";
try {
if(null != date){
str = new SimpleDateFormat(format).format(date);
}
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}