Java Time Processing Tool class

Source: Internet
Author: User
Tags tostring

1 Import Java.io.PrintStream;


2 import Java.sql.Time;


3 Import Java.sql.Timestamp;


4 Import java.text.ParsePosition;


5 import Java.text.SimpleDateFormat;


6 Import Java.util.Date;


7 public class Timeutil


8 {


9/*


Java Programming


11 * *


public Timeutil ()


13 {


14}


public static Date strtodate (String sStr)


16 {


if (sStr = null)


return null;


SimpleDateFormat Formatter;


if (sstr.length () = 19)


formatter = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");


Else


if (sstr.length () = 10)


formatter = new SimpleDateFormat ("Yyyy-mm-dd");


Else


if (sstr.length () = 8)


Formatter = new SimpleDateFormat ("YyyyMMdd");


Else


if (sstr.length () = 14)


formatter = new SimpleDateFormat ("Yyyymmddhhmmss");


Else


formatter = new SimpleDateFormat ("Yyyymmddhhmmss");


parseposition pos = new parseposition (0);


return Formatter.parse (SSTR, POS);


35}


public static Timestamp Strtodatetime (String s)


37 {


return to New Timestamp (Strtodate (s). GetTime ());


39}


public static String Datetimetochinese (Date dtsource)


41 {


SimpleDateFormat formatter = new SimpleDateFormat ("yyyy mm month DD Day hh when mm minute ss seconds");


return Formatter.format (Dtsource);


44}


public static String datetostr (date date)


46 {


SimpleDateFormat format = new SimpleDateFormat ("YyyyMMdd");


return Format.format (date);


49}


public static String datetimetostr (date date)


51 {


if (date = null)


53 {


"";


. Else


56 {


SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");


return Formatter.format (date);


59}


60}


public static String today ()


62 {


The return datetostr (New Date ());


64}


public static String now ()


66 {


return Datetimetostr (New Date ());


68}


public static string Calctime (string time, int diffyear, int diffmonth, int diffdate, int diffhour, int diffminut e, int diffsecond)


70 {


Timestamp Timestamp = Strtodatetime (time);


timestamp.setyear (timestamp.getyear () + diffyear);


Timestamp.setmonth (Timestamp.getmonth () + diffmonth);


timestamp.setdate (timestamp.getdate () + diffdate);


timestamp.sethours (timestamp.gethours () + diffhour);


timestamp.setminutes (timestamp.getminutes () + Diffminute);


timestamp.setseconds (timestamp.getseconds () + Diffsecond);


return datetimetostr (timestamp);


79}


public static String GetCurrentTime ()


81 {


the String s = (New Time (System.currenttimemillis ())). ToString ();


the return s;


84}


public static String getcurrentdate ()


86 {


a String s = (new Java.sql.Date (System.currenttimemillis ()). ToString ();


return s;


89}


The public static String Getcurrentnow ()


91 {


a String s = getcurrentdate () + "" + getcurrenttime ();


return s;


94}


public static string Datefromstr (string date)


96 {


(date = null | | date.length () < 10)


return getcurrentdate ();


Else


return date.substring (0, 10);


101}


102 public static string Timefromstr (String date)


103 {


(date = null | | date.length () < 19)


return "00:00:00";



Else

return date.substring (11, 19);


108}


109 public static void Main (String args[])


110 {


System.out.println (now ());


112}


113}

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.