Java custom date format contains other letters, java date format

Source: Internet
Author: User

Java custom date format contains other letters, java date format

I believe that many custom date formats can be found on the Internet, such as yyyy-MM-dd HH: mm: ss, yyyy-MM-dd HH, mm, ss, and so on. But if there are other letters in the date, how should we define the format? For example, 2014-11-03T12: 45: 20 is output, with one T character added. If the format is defined as: yyyy-MM-ddTHH: mm: ss, an error occurs when a line is run. The T character cannot be recognized.

The solution is to escape the T so that the system does not need to convert it. I tried \ T and the compilation failed. Then we use 'T', so we can get it right.


How to customize a date in java

Date date = new Date ();
SimpleDateFormat f = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); // hh: 12 hours HH: 24 hours
String now = f. format (date );

Write a java program and enter a string of letters ('A'-'Z'). If the string contains other letters, a custom exception is thrown.

Import java. util .*;
Public class Test
{
Public static void main (String [] args) throws MyException {
Pipeline SC = new pipeline (System. in );
System. out. println ("enter a string of characters 'a'-'Z '");
String str = SC. nextLine ();
If (str. matches ("^ [a-z] + $ ")){
System. out. println ("the string you entered is:" + str );
} Else {
Throw new MyException ("enter the correct English letter! ");
}
}
}

Class MyException extends Exception {
Public MyException (String e ){
Super (e );
System. out. println (e );
}
Public MyException (){}
}

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.