java日期格式匹配
![]()
这里是项目中封装的一个日期格式匹配的工具类
- package cn.zks.util;
-
- import java.text.DateFormat;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
-
- public class FormatDateUtil {
-
- public FormatDateUtil() {
- }
-
-
- static public String formatDateTime() {
-
- Date date = new Date();
-
- DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- return format.format(date);
- }
-
- static public String formatDateTime2() {
-
- Date date = new Date();
- DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
-
- return format.format(date);
- }
-
-
- static public Date formatDate() throws ParseException {
- Date date = new Date();
- DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String time = format.format(date);
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Date date2 = sdf.parse(time);
- return date2;
- }
-
-
- static public String fomatDate(String dateTime) throws ParseException {
- SimpleDateFormat sdf = new SimpleDateFormat(" yyyy-MM-dd HH:mm:ss ");
- Date date = sdf.parse(dateTime);
- DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- return format.format(date);
- }
-
-
- static public String fomatDate31(String dateTime) throws ParseException {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- Date date = sdf.parse(dateTime);
- DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
- return format.format(date);
- }
-
-
- static public String fomatDate3(String dateTime) throws ParseException {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- Date date = sdf.parse(dateTime);
- DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- return format.format(date);
- }
-
-
- static public Date fomatDate2(String dateTime) throws ParseException {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- Date date = sdf.parse(dateTime);
- return date;
- }
-
-
- static public Date fomatDate4(String dateTime) throws ParseException {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Date date = sdf.parse(dateTime);
- return date;
- }
-
-
- static public String formatDateTime(Date basicDate, String strFormat) {
- SimpleDateFormat df = new SimpleDateFormat(strFormat);
- return df.format(basicDate);
- }
-
-
- static public String formatDateTime(String basicDate, String strFormat) {
- SimpleDateFormat df = new SimpleDateFormat(strFormat);
- Date tmpDate = null;
- try {
- tmpDate = df.parse(basicDate);
- } catch (Exception e) {
-
- }
- return df.format(tmpDate);
- }
-
-
- static public String nDaysAftertoday(int n) {
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
- Calendar rightNow = Calendar.getInstance();
-
- rightNow.add(Calendar.DAY_OF_MONTH, +n);
- return df.format(rightNow.getTime());
- }
-
-
- static public Date nDaysAfterNowDate(int n) {
- Calendar rightNow = Calendar.getInstance();
-
- rightNow.add(Calendar.DAY_OF_MONTH, +n);
- return rightNow.getTime();
- }
-
-
- static public String nDaysAfterOneDateString(String basicDate, int n) {
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
- Date tmpDate = null;
- try {
- tmpDate = df.parse(basicDate);
- } catch (Exception e) {
-
- }
- long nDay = (tmpDate.getTime() / (24 * 60 * 60 * 1000) + 1 + n) * (24 * 60 * 60 * 1000);
- tmpDate.setTime(nDay);
-
- return df.format(tmpDate);
- }
-
-
- static public String nHoursAfterOneDateString(String basicDate, int n) {
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Date tmpDate = null;
- try {
- tmpDate = df.parse(basicDate);
- } catch (Exception e) {
-
- }
- long nDay = (tmpDate.getTime() / (60 * 60 * 1000) + n) * (60 * 60 * 1000);
- tmpDate.setTime(nDay);
-
- return df.format(tmpDate);
- }
-
-
- static public String nHoursBeforeOneDateString(String basicDate, int n) {
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Date tmpDate = null;
- try {
- tmpDate = df.parse(basicDate);
- } catch (Exception e) {
-
- }
- long nDay = (tmpDate.getTime() / (60 * 60 * 1000) - n) * (60 * 60 * 1000);
- tmpDate.setTime(nDay);
-
- return df.format(tmpDate);
- }
-
-
- static public Date nDaysAfterOneDate(Date basicDate, int n) {
- long nDay = (basicDate.getTime() / (24 * 60 * 60 * 1000) + 1 + n) * (24 * 60 * 60 * 1000);
- basicDate.setTime(nDay);
-
- return basicDate;
- }
-
-
- static public int nDaysBetweenTwoDate(Date firstDate, Date secondDate) {
- int nDay = (int) ((secondDate.getTime() - firstDate.getTime()) / (24 * 60 * 60 * 1000));
- return nDay;
- }
-
-
- static public int nDaysBetweenTwoDate(String firstString, String secondString) {
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
- Date firstDate = null;
- Date secondDate = null;
- try {
- firstDate = df.parse(firstString);
- secondDate = df.parse(secondString);
- } catch (Exception e) {
-
- }
-
- int nDay = (int) ((secondDate.getTime() - firstDate.getTime()) / (24 * 60 * 60 * 1000));
- return nDay;
- }
-
-
- static public int nHoursBetweenTwoDate(String firstString, String secondString) {
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Date firstDate = null;
- Date secondDate = null;
- try {
- firstDate = df.parse(firstString);
- secondDate = df.parse(secondString);
- } catch (Exception e) {
-
- }
-
- int nDay = (int) ((secondDate.getTime() - firstDate.getTime()) / (60 * 60 * 1000));
- return nDay;
- }
- }
原文地址http://www.bieryun.com/3180.html