Tool classes for string and date type conversions

Source: Internet
Author: User
Tags dateformat string methods

Common string-to-date, and date-to-string methods ...



Package Com.cq2022.zago.base.util;import Java.text.dateformat;import Java.text.parseexception;import Java.text.simpledateformat;import Java.util.date;import Java.util.gregoriancalendar;import Javax.xml.datatype.datatypefactory;import Javax.xml.datatype.xmlgregoriancalendar;import Org.slf4j.Logger;import org.slf4j.loggerfactory;/*** * Date Tool class * * @author shijing * */public class Dateutils {private static final Logger Logger = Loggerfactory.getlogger (dateutils.class);/*** * Date type converted to Xmlgregoriancalendar type * * @param date * @return */public stat IC Xmlgregoriancalendar Converttoxmlgregoriancalendar (date date) {GregorianCalendar cal = new GregorianCalendar (); Cal.settime (date); Xmlgregoriancalendar GC = null;try {GC = Datatypefactory.newinstance (). Newxmlgregoriancalendar (CAL);} catch (Exception e) {logger.error ("date type converted to Xmlgregoriancalendar type error:" +e);} return GC;} /*** * Xmlgregoriancalendar type converted to date type * * @param cal * @return * @throws Exception */public static date converttodate (XM LgregorianCalendar cal) throws Exception {GregorianCalendar CA = Cal.togregoriancalendar (); return Ca.gettime ();} /** * String to date * March 25, 2015 Morning 11:27:14 * auther:shijing * @param str Date String * @param format conversion format * @return * Date */pub Lic static date stringtodate (string str, string format) {DateFormat DateFormat = new SimpleDateFormat (format);D ate Date = null;try {date = Dateformat.parse (str);} catch (ParseException e) {logger.error ("string type goes to date type error:" +e);} return date;} /** * Date Turn String * auther:shijing * March 25, 2015 Morning 11:28:14 * @param date * @param format conversion format * @return */public Stat IC String datetostring (Date date,string format) {DateFormat DateFormat = new SimpleDateFormat (format); String Strdate=null;try {if (date!=null) {Strdate=dateformat.format (date);}} catch (Exception e) {//TODO auto-generated Catch Blocklogger.error ("date type goes to String type error:" +e);} return strdate;} /*** * Test Method * * @param args */public static void main (string[] args) {Xmlgregoriancalendar d = Dateutils.converttoxmlgreg OriaNcalendar (New Date ());//system.out.println (D.getday ()); Xmlgregoriancalendar cal = Null;try {cal = Datatypefactory.newinstance (). Newxmlgregoriancalendar (); Cal.setMonth (06); Cal.setyear;D ate Date = Dateutils.converttodate (cal); String format = "Yyyy-mm-dd HH:mm:ss"; SimpleDateFormat formatter = new SimpleDateFormat (format),//system.out.println (date);D ate formatter.format = Dateutils.stringtodate ("2014/7/24 9:51:00", "Yyyy/mm/dd hh:mm:ss"); Xmlgregoriancalendar D2 = Dateutils.converttoxmlgregoriancalendar (D1);//system.out.println (d2.toString ()); String datestr=dateutils.datetostring (Cal.togregoriancalendar (). GetTime (), "Yyyy-mm-dd HH:mm:ss");// System.out.println ("datestr=" + Datestr);} catch (Exception e) {e.printstacktrace ();}}}

Tool classes for string and date type conversions

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.