Java Time Date Format Tool class program

Source: Internet
Author: User
Tags current time dateformat string format

Example 1. Organize a reusable date formatting tool class that is not necessary for programmers who are miserable in daily development
Here's how to contribute to the Java Date Tool class code:

The code is as follows Copy Code

/**
* Date Tool class-www.111cn.net network finishing
* The default use of the "YYYY-MM-DD HH:mm:ss" format date
* @author XW Material Network
*/
Public final class Dateutils {
/**
* English abbreviation (default) such as: 2010-12-01
*/
public static String Format_short = "YYYY-MM-DD";
/**
* English full name: 2010-12-01 23:15:06
*/
public static String Format_long = "Yyyy-mm-dd HH:mm:ss";
/**
* Accurate to millisecond full time such as: Yyyy-mm-dd HH:mm:ss. S
*/
public static String Format_full = "Yyyy-mm-dd HH:mm:ss. S ";
/**
* Chinese abbreviation: December 01, 2010
*/
public static String FORMAT_SHORT_CN = "yyyy year mm month DD";
/**
* Chinese Full name: December 01, 2010 23:15 06 seconds
*/
public static String FORMAT_LONG_CN = "yyyy mm month DD Day hh when the minute of SS seconds";
/**
* Accurate to millisecond full Chinese time
*/
public static String FORMAT_FULL_CN = "yyyy mm month DD Day hh when the minute is SS seconds sss milliseconds";
/**
* Get the default date pattern
*/
public static String Getdatepattern () {
return format_long;
}
/**
* Return current date according to preset format
* @return
*/
public static String Getnow () {
Return format (new Date ());
}
/**
* Return current date according to user format
* @param format
* @return
*/
public static string Getnow (string format) {
Return format (new Date (), format);
}
/**
* Format dates with preset formatting
* @param date
* @return
*/
public static String format (date date) {
return format (date, Getdatepattern ());
}
/**
* Format dates with user format
* @param date
* @param pattern Date format
* @return
*/
public static string format (date date, String pattern) {
String returnvalue = "";
if (date!= null) {
SimpleDateFormat df = new SimpleDateFormat (pattern);
returnvalue = Df.format (date);
}
return (returnvalue);
}
/**
* Extract string date using preset format
* @param strdate Date string
* @return
*/
public static Date Parse (String strdate) {
Return Parse (strdate, Getdatepattern ());
}
/**
* Extract string date using user format
* @param strdate Date string
* @param pattern Date format
* @return
*/
public static Date Parse (string strdate, string pattern) {
SimpleDateFormat df = new SimpleDateFormat (pattern);
try {
Return Df.parse (strdate);
catch (ParseException e) {
E.printstacktrace ();
return null;
}
}
/**
* Add a few month to the date
* @param date
* @param n to increase the number of months
* @return
*/
public static date Addmonth (date date, int n) {
Calendar cal = Calendar.getinstance ();
Cal.settime (date);
Cal.add (Calendar.month, N);
return Cal.gettime ();
}
/**
* Increase the number of days on the date
* @param date
* @param n to increase the number of days
* @return
*/
public static date Addday (date date, int n) {
Calendar cal = Calendar.getinstance ();
Cal.settime (date);
Cal.add (calendar.date, N);
return Cal.gettime ();
}
/**
* Get time stamp
*/
public static String gettimestring () {
SimpleDateFormat df = new SimpleDateFormat (format_full);
Calendar calendar = Calendar.getinstance ();
Return Df.format (Calendar.gettime ());
}
/**
* Get date year
* @param date
* @return
*/
public static String getyear (date date) {
return format (date). substring (0, 4);
}
/**
* By default format string distance today's number of days
* @param date string
* @return
*/
public static int countdays (String date) {
Long T = calendar.getinstance (). GetTime (). GetTime ();
Calendar C = calendar.getinstance ();
C.settime (Parse (date));
Long T1 = C.gettime (). GetTime ();
return (int) (t/1000-t1/1000)/3600/24;
}
/**
* by user format string distance today's days
* @param date string
* @param format Date
* @return
*/
public static int Countdays (string date, string format) {
Long T = calendar.getinstance (). GetTime (). GetTime ();
Calendar C = calendar.getinstance ();
C.settime (Parse (date, format));
Long T1 = C.gettime (). GetTime ();
return (int) (t/1000-t1/1000)/3600/24;
}
}

Example 2. Not only has the above function, but also contains the commonly used date formatting method

The code is as follows Copy Code

Package com.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;

Public class Commondateparseutil {
 public static final String Eng_date_fromat = "EEE, d MMM yyyy z";
 public static final String yyyy_mm_dd_hh_mm_ss = "Yyyy-mm-dd HH:mm:ss";
 public static final String yyyy_mm_dd_hh_mm = "Yyyy-mm-dd hh:mm";
 public static final String yyyy_mm_dd = "YYYY-MM-DD";
 public static final String YYYY = "YYYY";
 public static final String mm = "mm";
 public static final String dd = "DD";

 /**
  * @param date
  * @return
  * @ author Wang jianming
  * @ creation Date 2012-7-13
  * @ Create Time 12:22:40
  * Description--Format Date Object
  */
 public static date date2date (date date, String format STR) {
  simpledateformat SDF = new SimpleDateFormat (FORMATSTR);
  string str = Sdf.format ( Date);
  try {
   date = sdf.parse (str);
  } catch (Exception e) {
 &nb Sp; return null;
  }
  return date;
 }

/**
* @param date
* @return
* @ Author Wang Jianming
* @ Creation Date 2012-7-13
* @ creation Time 12:24:19
* Description--time object converted to string
*/
public static string date2string (date date, String formatstr) {
String strdate = "";
SimpleDateFormat SDF = new SimpleDateFormat (FORMATSTR);
Strdate = Sdf.format (date);
return strdate;
}

/**
* @param date
* @return
* @ Author Wang Jianming
* @ Creation Date 2012-7-13
* @ creation Time 12:24:19
* @ Description--sql Time object converted to string
*/
public static string timestamp2string (Timestamp Timestamp, string formatstr) {
String strdate = "";
SimpleDateFormat SDF = new SimpleDateFormat (FORMATSTR);
Strdate = Sdf.format (timestamp);
return strdate;
}

 /**
  * @param datestring
  * @param formatstr
  * @return
  * @ author Wang jianming
&n Bsp * @ Creation Date 2012-7-13
  * @ creation time 1:09:24
  * @ Description--string converted to time object
  */
 public static Date s Tring2date (String datestring, String formatstr) {
  date formatedate = null;
  dateformat format = new SimpleDateFormat (FORMATSTR);
  try {
   formatedate = Format.parse (datestring);
  } catch ( ParseException e) {
   return null;
  }
  return formatedate;
 .}

/**
* @param date
* @return
* @ Author Wang Jianming
* @ Creation Date 2012-10-10
* @ creation Time 09:18:36
* @ description--date type conversion to timestamp type
*/
public static Timestamp Date2timestamp (date date) {
if (date = null)
return null;
return new Timestamp (Date.gettime ());
}

/**
* @return
* @ Author Wang Jianming
* @ Creation Date 2012-9-13
* @ creation Time 05:02:57
* Description--Get current year
*/
public static String Getnowyear () {
SimpleDateFormat SDF = new SimpleDateFormat (YYYY);
Return Sdf.format (New Date ());
}

 /**
  * @return
  * @ author Wang jianming
  * @ creation Date 2012-9-13
  * @ creation time PM 05:03:15
&nbs P * @ Description--get current month
  */
 public static String Getnowmonth () {
  simpledateformat sdf = new Si Mpledateformat (MM);
  return Sdf.format (New Date ());
 }
 
 /**
  * @return
  * @ author Wang jianming
  * @ creation Date 2013-01-24
  * @ creation time 08:41 :
  * @ description--Get day in current date
  */
 public static String Getnowday () {
  simpledateform At SDF = new SimpleDateFormat (DD);
  return Sdf.format (New Date ());
 }

 /**
  * @param time
  * @return
  * @ author Wang jianming
  * @ creation Date 2012-6-17
  * @ creation Time 10:19:31
  * @ description--Chinese information for specified time distance at current time
  */
 public static String Getlnow (long time) {
  calendar cal = Calendar.getinstance ();
  long Timel = Cal.gettimeinmillis ()-time;
  if (timel/1000 <) {
   return "within 1 minutes";
  } else if (timel/1000 {<) {
   return timel/1000/60 + minutes ago;
  } else if (timel/1000/60/60 <) {
   return timel/1000/60/60 + hours ago;
  } else {
   re Turn timel/1000/60/60/24 + "days ago";
  }
 }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.