Java uses simpledateformat to format the time.

Source: Internet
Author: User

During the past two days, the book management system, in the book borrowing record, the month recorded in the time always shows abnormal values, more than 12 often 40 to 50, after tracking the borrowing module, the error is finally located in the line of Time-formatted code.

Simpledateformat F = newsimpledateformat ("yyyy-mm-dd hh: mm: SS ");

After this line of code is to format the format to the common 09:07:20 format, the results often appear 09:07:20 or 2013-34-110, the month is very strange, and then found yyyy-mm-DDHH: MM: The mm of the month in SS is lower-case. As a result, the time of the minute will be placed in the month position during formatting. It is strange that the month is changed

Simpledateformatf = new simpledateformat ("yyyy-mm-dd hh: mm: SS ");

After that, the time will be normal.

Although very simple, but prompt me, a simple line of code may also be wrong.

 

The following is a record of simpledateformat usage.

Java code:

Import Java. text. simpledateformat; simpledateformat formatter = new simpledateformat ("yyyy-mm-dd hh: mm: SS"); Date curdate = new date (system. currenttimemillis (); // obtain the current time string STR = formatter. format (curdate );


You can obtain the current date and month, or write them together (as follows ):
Java code:

 

SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");String date = sDateFormat.format(new java.util.Date());

If you want to obtain the current year and month, you can write it like this (only get the same time or second usage ):
Java code:

 

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM");String date=sdf.format(new java.util.Date());

Of course, you can also specify the time zone (to be ):

 

df=DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL,Locale.CHINA);System.out.println(df.format(new Date()));

How to obtain whether the Android system is in the 24-hour or 12-hour format:
  

ContentResolver cv = this.getContentResolver();    String strTimeFormat = android.provider.Settings.System.getString(cv,  android.provider.Settings.System.TIME_12_24);  if(strTimeFormat.equals("24"))  {  Log.i("activity","24");  }

 

Date of obtaining the system:

Calendar c = Calendar.getInstance();year = c.get(Calendar.YEAR);month = c.grt(Calendar.MONTH);day = c.get(Calendar.DAY_OF_MONTH);hour = c.get(Calendar.HOUR_OF_DAY);minute = c.get(Calendar.MINUTE);


Obtain with time:

Time t = new time (); // or time t = new time ("GMT + 8"); plus the time zone information. T. settonow (); // obtain the system time. Int year = T. year; int month = T. month; int date = T. monthday; int hour = T. hour; // 0-23 int minute = T. minute; int second = T. second; // The only disadvantage is that the retrieval time is only in 24-hour mode.

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.