Java Date Format Conversion Tool

Source: Internet
Author: User
Tags date now dateformat
Package COM. ace. backOffice. utils; import Java. text. parseexception; import Java. text. simpledateformat; import Java. util. calendar; import Java. util. date; import javax. XML. datatype. datatypeconfigurationexception; import javax. XML. datatype. datatypefactory; import javax. XML. datatype. xmlgregoriancalendar; public class dateutils {/*** converts a specified string to a date ** @ Param date * string date string * @ Param datepattern * string Date Format * @ Return date */public static Java. util. date getformatdate (string date, string datepattern) {simpledateformat SD = new simpledateformat (datepattern); Return SD. parse (date, new Java. text. parseposition (0 ));} /*** convert a specified date object to a formatted string ** @ Param date * Date XML date object * @ Param datepattern * string date format * @ return string */public static string getformattedstring (date, string datepattern) {simpledateforma T SD = new simpledateformat (datepattern); Return SD. format (date );} /*** convert a specified XML date object to a formatted string ** @ Param xmldate * Date XML date object * @ Param datepattern * string date format * @ return string */public static string getformattedstring (xmlgregoriancalendar xmldate, string datepattern) {simpledateformat SD = new simpledateformat (datepattern); Calendar calendar = xmldate. togregoriancalendar (); Return SD. format (calendar. Gettime ());} /*** convert a specified XML date object to a date object ** @ Param xmldate * Date XML date object * @ Param datepattern * string date format * @ return date */public static date xmlgregoriancalendar2date (xmlgregoriancalendar xmldate) {return xmldate. togregoriancalendar (). gettime ();} public static string getthisyear () {// obtain the current date calendar cldcurrent = calendar. getinstance (); // get year month day string stryear = string. valueof (cldcurrent. get (calendar. Year); Return stryear;} public static xmlgregoriancalendar convert2xmlcalendar (calendar) {try {datatypefactory DTF = datatypefactory. newinstance (); Return DTF. newxmlgregoriancalendar (calendar. get (calendar. year), calendar. get (calendar. month) + 1, calendar. get (calendar. day_of_month), calendar. get (calendar. hour), calendar. get (calendar. minute), calendar. get (calendar. second), calendar. get (calendar. mill Isecond), calendar. get (calendar. zone_offset)/(1000*60);} catch (datatypeconfigurationexception e) {e. printstacktrace (); return NULL ;}// obtain public static Java on the current day. SQL. timestamp getnowtime (string dateformat) {date now = new date (); simpledateformat dateformat = new simpledateformat (dateformat); // you can easily modify the Date Format String datestring = dateformat. format (now); simpledateformat SD = new simpledateformat (dateforma T); Date dateformt = SD. parse (datestring, new Java. text. parseposition (0); Java. SQL. timestamp datetime = new Java. SQL. timestamp (dateformt. gettime (); Return datetime; // return hehe;} // obtain public static Java at the specified time. SQL. timestamp getnownewtime (string date, string dateformat) {// Date Now = new date (); simpledateformat dateformat = new simpledateformat (dateformat); // you can easily modify the date format dateformat. parse (date, new Java. Text. parseposition (0); // string datestring = dateformat. format (date); Date dateformt = dateformat. parse (date, new Java. text. parseposition (0); Java. SQL. timestamp datetime = new Java. SQL. timestamp (dateformt. gettime (); Return datetime; // return hehe;}/*** @ Param contains yyyy-mm-dd't'hh: mm: Ss. time conversion in SSS format. * @ return */public static string gettformatstring (string tdate) {simpledateformat format1 = new Si Mpledateformat ("yyyy-mm-dd't'hh: mm: Ss. SSS "); string STR =" "; try {Java. util. date = format1.parse (tdate); simpledateformat format2 = new simpledateformat ("yyyy-mm-dd hh: mm: SS"); STR = format2.format (date );} catch (parseexception e) {e. printstacktrace ();} return STR;} public static void main (string [] ARGs) {simpledateformat SD = new simpledateformat ("yyyymmdd"); string date = "20110202"; system. out. PR Intln (SD. parse (date, new Java. text. parseposition (0); system. out. println (getbefore2hourdate ();} // obtain the first two hours of the current time. Public static string getbefore2hourdate () {simpledateformat df = new simpledateformat ("yyyy-mm-dd't'hh: mm: SS"); Calendar c = calendar. getinstance (); C. add (calendar. hour_of_day,-2); // Add 3 hours to return DF. format (C. gettime ();}/***** @ Param time1 current time * @ Param time2 comparison time * @ return if time1 is greater than time2 gap minutes, true is returned; */public static Boolean comparedatetime (date time1, date time2, int gap) {return time1.gettime ()-time2.gettime ()> GaP * 60*1000 ;}}

Related Article

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.