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 (){}
}