Java: String and Date, Timestamp transfer between
a, String and Date (java.util.Date) Huzhuan
1.1 String -> Date
- String dateStr = "2010/05/04 12:34:23" ;
- Date date = < span class = "keyword" style = "color: # 7f0055; font-weight: bold;"> new Date ();
- / / note format and date format to match the format String
- DateFormat sdf = < span class = "keyword" style = "color: # 7f0055; font-weight: bold;"> new SimpleDateFormat ( "yyyy / MM / dd HH: mm: ss" );
- try {
- date = sdf.parse (dateStr);
- System.out.println (date.toString ());
- } catch (Exception e) {
- e.printStackTrace ();
- }
1.2 Date -> String
date to string conversion, you can set any format conversion format
- String dateStr = "" ;
- Date date = < span class = "keyword" style = "color: # 7f0055; font-weight: bold;"> new Date ();
- / / format can be any format
- DateFormat sdf = < span class = "keyword" style = "color: # 7f0055; font-weight: bold;"> new SimpleDateFormat ( "yyyy / MM / dd HH: mm: ss" );
- DateFormat sdf2 = < span class = "keyword" style = "color: # 7f0055; font-weight: bold;"> new SimpleDateFormat ( "yyyy-MM-dd HH / mm / ss" );
- try {
- dateStr = sdf.format (date);
- System.out.println (dateStr);
- dateStr = sdf2.format (date);
- System.out.println (dateStr);
- } catch (Exception e) {
- e.printStackTrace ();
- }
two, String and Timestamp referrals
2.1 String -> Timestamp
use the Timestamp valueOf () method
- Timestamp ts = new Timestamp (System.currentTimeMillis ());
- String tsStr = < span class = "string" style = "color: blue;"> "2011-05-09 11:49:45" ; < / span>
- try {
- ts = Timestamp.valueOf (tsStr);
- System.out.println (ts);
- } catch (Exception e) {
- e.printStackTrace ();
- }
; Note: String type must be of the form: yyyy-mm-dd hh: mm: ss [. f. ..] such a format, brackets indicate optional, otherwise an error! ! !
If the String to other formats, you can consider re-parse the string under and then re ~ ~
2.2 Timestamp -> String
Timestamp use the toString () method or borrow DateFormat
- Timestamp ts = new Timestamp (System.currentTimeMillis ());
- String tsStr = < span class = "string" style = "color: blue;"> "" ;
- DateFormat sdf = < span class = "keyword" style = "color: # 7f0055; font-weight: bold;"> new SimpleDateFormat ( "yyyy / MM / dd HH: mm: ss" );
- try {
- / / Method One
- tsStr = sdf.format (ts);
- System.out.println (tsStr);
- / / method two
- tsStr = ts.toString ();
- System.out.println (tsStr);
- } catch (Exception e) {
- e.printStackTrace ();
- }
was easily able to see it, the method has the advantage of a flexible set of strings can form.
three, Date ( java.util.Date ) and Timestamp referrals
statement: check API shows, Date and Timesta parent-child class relationships
3.1 Timestamp -> Date
- Timestamp ts = new Timestamp (System.currentTimeMillis ());
- Date date = < span class = "keyword" style = "color: # 7f0055; font-weight: bold;"> new Date ();
- try {
- date = ts;
- System.out.println (date);
- } catch (Exception e) {
- e.printStackTrace ();
- }
very simple, but at the moment the date object is an entity pointing Timestamp, that date has a Date class methods, but the method of execution was covered entities Timestamp medium.
3.2 Date -> Timestamp
parent class can not be converted directly to the sub-class, can make use of in the middle of String ~ ~ ~ ~
Note: Use the following method is more simple
Timestamp ts = new Timestamp (date.getTime ());
没有评论:
发表评论