Java date tool class

Source: Internet
Author: User

Import java. text. parseException; import java. text. simpleDateFormat; import java. util. date; import java. util. timeZone; public class DateUtils {/** definition constant **/public static final String DATE_JFP_STR = "yyyyMM"; public static final String DATE_FULL_STR = "yyyy-MM-dd HH: mm: ss "; public static final String DATE_SMALL_STR =" yyyy-MM-dd "; public static final String DATE_KEY_STR =" yyMMddHHmmss "; /*** extract the String Date using the preset format * @ param strDate Date String * @ return */public static Date parse (String strDate) {return parse (strDate, DATE_FULL_STR );} /*** extract String Date in 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 ;}/ *** two time comparisons * @ param date * @ return */public static int compareDateWithNow (Date date1) {Date date2 = new Date (); int rnum = date1.compareTo (date2); return rnum;}/*** two time comparisons (timestamp comparison) * @ param date * @ return */public static int compareDateWithNow (long date1) {long date2 = dateToUnixTimestamp (); if (date1> date2) {return 1 ;} else if (date1 <date2) {return-1 ;}else {return 0 ;}/ *** get the current system time * @ return */public static String getNowTime () {SimpleDateFormat df = new SimpleDateFormat (DATE_FULL_STR); return df. format (new Date ();}/*** get the current system time * @ return */public static String getNowTime (String type) {SimpleDateFormat df = new SimpleDateFormat (type ); return df. format (new Date ();}/*** get the current billing period of the system * @ return */public static String getJFPTime () {SimpleDateFormat df = new SimpleDateFormat (DATE_JFP_STR ); return df. format (new Date ();}/*** converts the specified date to a Unix timestamp * @ param String Date. The date to be converted is yyyy-MM-dd HH: mm: ss * @ return long timestamp */public static long dateToUnixTimestamp (String date) {long timestamp = 0; try {timestamp = new SimpleDateFormat (DATE_FULL_STR ). parse (date ). getTime ();} catch (ParseException e) {e. printStackTrace ();} return timestamp ;} /*** convert the specified date to Unix timestamp * @ param String date the date to be converted to yyyy-MM-dd * @ return long timestamp */public static long dateToUnixTimestamp (string date, string dateFormat) {long timestamp = 0; try {timestamp = new SimpleDateFormat (dateFormat ). parse (date ). getTime ();} catch (ParseException e) {e. printStackTrace ();} return timestamp;}/*** converts the current date to a Unix timestamp * @ return long timestamp */public static long dateToUnixTimestamp () {long timestamp = new Date (). getTime (); return timestamp ;} /*** convert Unix timestamp to date * @ param long timestamp * @ return String date String */public static String unixTimestampToDate (long timestamp) {SimpleDateFormat sd = new SimpleDateFormat (DATE_FULL_STR); sd. setTimeZone (TimeZone. getTimeZone ("GMT + 8"); return sd. format (new Date (timestamp ));}}

This article from the "Dream think Xi" blog, please be sure to keep this source http://qiangmzsx.blog.51cto.com/2052549/1305082

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.