Java Date and SimpleDateFormat

Source: Internet
Author: User

I've been working on some projects that involve time processing. Originally wrote a time conversion function, although can be used but too cumbersome and not standardized, so learned the next Java comes with the time processing class.

 Public classTimechg { Public Static intymd[][][]=New int[110] [13] [33];  Public Static intDay[][][] =New int[25] [61] [61];  Public Static int_ymd[][] =New int[110*13*33+1] [3];  Public Static int_day[][] =New int[25*61*61+1] [3];  Public Static intSave[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};  Public Final Static intOnedays = 24*60*60;  Public Static voidTimeinit () {intY=2000,m=1,d=1; intCnt=0; Ymd[y-2000][M][D] =CNT; _ymd[cnt][0]=y;_ymd[cnt][1]=m; _ymd[cnt][2]=D;  while(y<2100)        {            if((y%4==0&&y%100!=0) | | (y%400==0)) save[2]=29; ElseSave[2]=28; D++; CNT++; if(d>Save[m]) {m++; D=1; if(m>12) {y++; M=1; }} ymd[y-2000][M][D] =CNT; _ymd[cnt][0]=y;_ymd[cnt][1]=m; _ymd[cnt][2]=D; }                intH=0,s=0; M= 0;//hours, minutes, secondsCnt=0; Day[h][m][s]=CNT; _day[cnt][0]=h; _day[cnt][1]=m; _day[cnt][2]=s;  while(true) {CNT++; S++; if(s==60) {s=0; M++; if(m==60) {m=0; H++; if(h==24)                    {                         Break; }}} Day[h][m][s]=CNT; _day[cnt][0]=h; _day[cnt][1]=m; _day[cnt][2]=s; }            }    /*** Time is in the format YYYYMMDDHHMMSS *@paramTime *@return     */     Public Static intStrtoint (String time) {inty,m,d,h,m,s; Y= Integer.parseint (time.substring (0, 4) ); M= Integer.parseint (Time.substring (4, 6) ); D= Integer.parseint (time.substring (6, 8) ); H= Integer.parseint (time.substring (8, 10) ); M= Integer.parseint (Time.substring (10, 12) ); S= Integer.parseint (Time.substring (12, 14) ); returnymd[y-2000<0?0:y-2000][m][d]*onedays +Day[h][m][s]; }         Public StaticString IntToStr (intTime ) {StringBuffer Timestr=NewStringBuffer (""); intIntymd,intday; Intymd= time/onedays; Intday= Time%onedays; Timestr.append (String.Format ("%04d", _ymd[intymd][0])); Timestr.append (String.Format ("%02d", _ymd[intymd][1])); Timestr.append (String.Format ("%02d", _ymd[intymd][2])); Timestr.append (String.Format ("%02d", _day[intday][0])); Timestr.append (String.Format ("%02d", _day[intday][1])); Timestr.append (String.Format ("%02d", _day[intday][2])); returntimestr.tostring (); }            }
Junk Code

Example 1 format output current system time

Date date=new  date ();//gets current time SimpleDateFormat df=new simpledateformat ("Yyyy-mm-dd hh:mm: ss

Example 2 converting a string format time to a timestamp

SimpleDateFormat SimpleDateFormat =new simpledateformat ("Yyyy-mm-dd-hh-mm-ss");D ate Date=  Null; Try {    = simpledateformat.parse ("2010-06-25-00-00-00"catch  (ParseException e) {     // TODO auto-generated Catch block     e.printstacktrace ();} long time = date.gettime ();//Starting January 1, 1970 the exact millisecond, to get the second unit, need/1000system.out.println (time);

Example 3 gets the date within the date object of the month and day, etc.

        Date date=new  date ();         System.out.println (Date.tostring ());        System.out.println (Date.getyear ()+1900);        System.out.println (Date.getmonth ()+1);        System.out.println (Date.getdate ()); // Note that Date.getday () is obtained day of the week, 0-6 respectively from Sunday, Monday, ... , Saturday

Java Date and SimpleDateFormat

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.