Javalearning: Date Action class

Source: Internet
Author: User
Tags stringbuffer

Package Org.fun.classdemo;import Java.util.calendar;import Java.util.gregoriancalendar;public class DateTime { Private Calendar calendar = new GregorianCalendar (); Instantiate the Calendar object public String getDate () {//2014-07-30stringbuffer buf = new StringBuffer (); Buf.append (Calendar.get ( calendar.year). Append ("-"); Buf.append (This.addzero ((Calendar.get (calendar.month) + 1), 2)). Append ("-"); Buf.append (This.addzero (Calendar.get (Calendar.day_of_month), 2)); return buf.tostring ();} Public String GetDateTime () {//2014-07-30 10:19:34.123stringbuffer buf = new StringBuffer (); Buf.append (This.getdate ()) . Append (""); Buf.append (This.addzero (Calendar.get (Calendar.hour_of_day), 2)). Append (":"); Buf.append (This.addzero (Calendar.get (Calendar.minute), 2)). Append (":"); Buf.append (This.addzero (Calendar.get (Calendar.second), 2)). Append ("."); Buf.append (This.addzero (Calendar.get (Calendar.millisecond), 3)); return buf.tostring ();} Public String Getdatecomplete () {//July 30, 2014 StringBuffer buf = new StringBuffer (); Buf.appEnd (Calendar.get (Calendar.year)). Append ("Year"), Buf.append (This.addzero (Calendar.get (calendar.month) + 1), 2)). Append ("month"); Buf.append (This.addzero (Calendar.get (Calendar.day_of_month), 2)). Append ("Day"); return buf.tostring ();} Public String Getdatetimecomplete () {//July 30, 2014 10:19 34 sec 123 msec StringBuffer buf = new StringBuffer (); Buf.append ( This.getdatecomplete ()); Buf.append (This.addzero (Calendar.get (Calendar.hour_of_day), 2)). Append ("time"); Buf.append ( This.addzero (Calendar.get (Calendar.minute), 2)). Append ("min"); Buf.append (This.addzero (Calendar.get ( Calendar.second), 2). Append ("Seconds"), Buf.append (This.addzero (Calendar.get (Calendar.millisecond), 3)). Append ("milliseconds"); return buf.tostring ();} Private String addzero (int temp, int len) {stringbuffer str = new StringBuffer (); str.append (temp);//increment number while (str.length () < Len) {str.insert (0, 0);//In the first position, add the letter 0}return str.tostring ();} public static void Main (String args[]) {System.out.println (New DateTime (). GetDate ()); System.out.println (New DateTime (). GetDateTime ()); System.out.println (New DateTime (). Getdatecomplete ()); System.out.println (New DateTime (). Getdatetimecomplete ());}}

Javalearning: Date Action class

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.