Setdate (date date) method in Java and conversion between string and Date

Source: Internet
Author: User
Tags dateformat

Often in the development process encountered such a problem, the data read from the database needs to be converted to a pair of image or Java bean, at this time is often used to setdate (date date), such a method. It feels like a simple and uncomfortable thing to do, and wasting time here really shouldn't. I am here to record what I have used, convenient for later access to use.

For example, the following setter method:

    PrivateString userId; Private BooleanIsLogin; PrivateDate Logintime;  Public voidsetlogintime (date logintime) {Date Oldlogintime= This. Logintime;  This. Logintime =Logintime; }     Public voidSetislogin (BooleanIsLogin) {        BooleanOldislogin = This. IsLogin;  This. IsLogin =IsLogin; }     Public voidSetuserid (String userId) {string Olduserid= This. userId;  This. UserId =userId; }

Used to convert a string to the date method

ImportJava.sql.Timestamp;ImportJava.text.DateFormat;Importjava.text.ParseException;ImportJava.text.SimpleDateFormat;ImportJava.util.Calendar;Importjava.util.Date; Public classDateutil {/*** The Set method is the Timestamp type * New Timestamp (s) *@returns*/     PublicTimestamp Newstamptime () {Longs=System.currenttimemillis (); return NewTimestamp (s); }    /*** Timestamp converted to String *@return     */     PublicString timestamptostring () {SimpleDateFormat DF=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");//define the format without displaying millisecondsTimestamp now =NewTimestamp (System.currenttimemillis ());//Get system Current timeString str =Df.format (now); returnstr; }    /*** String conversion to timestamp *@return     */     PublicTimestamp Stringtotimestamp () {SimpleDateFormat DF=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); String Time= Df.format (NewDate ()); Timestamp TS=timestamp.valueof (time); returnts; }     PublicDate Mystringtotimestamp (String datestring) {intLength=datestring.length (); if(length>10) {DateFormat df=NewSimpleDateFormat ("Yyyy-mm-dd"); Date Today=NewDate (); Try{Today= Df.parse ("2009-11-11")); //String Ts=df.format (today);}Catch(ParseException e) {e.printstacktrace (); }            returntoday; }Else{DateFormat DF=NewSimpleDateFormat ("Yyyy-mm-dd"); Date Today=NewDate (); Try{Today= Df.parse ("2009-11-11")); //String Ts=df.format (today);}Catch(ParseException e) {e.printstacktrace (); }            returntoday; }            }    /*** String converted to Timestamp * string "2016-5-25" to Timestamp *@return           */           PublicTimestamp Stringtotimestamp (String datestring) {SimpleDateFormat df=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); StringBuffer DS=NewStringBuffer (datestring); if(Datestring.length () >10) {Timestamp TS=timestamp.valueof (datestring); returnts; }Else{ds.append ("00:00:00"); Timestamp TS=timestamp.valueof (ds.tostring ()); returnts; }          }    /*** Get current time String * eg:2016-10-11 16:57:52 **/     PublicString formatedatestring () {SimpleDateFormat format=NewSimpleDateFormat ("Yyyy-mm-dd h:m:s"); String datestring=format.format (NewDate ()); returndatestring; }    /*** Specify time and current time comparison size*/     Public Booleancomparetime (String t1) {if(T1.length () <=10) {T1=t1+ "23:59:59"; } Date currenttime=NewDate ();//Current TimeSimpleDateFormat formatter =NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); String Nowtime=Formatter.format (currenttime); Calendar Now=calendar.getinstance (); Calendar C1=calendar.getinstance (); //String t1 = "2016-02-29 00:30:00";                Try{now.settime (Formatter.parse (nowtime));                C1.settime (Formatter.parse (t1)); } Catch(ParseException e) {e.printstacktrace (); }                intRESULT1 = Now.compareto (C1);//smaller than start time, not startedSYSTEM.OUT.PRINTLN ("Result:" +RESULT1); if(result1>0){                    return false; }Else{                    return true; }               }          }

Setdate (date date) method in Java and conversion between string and Date

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.