Java in the Date and String conversion

// Date to String

// Tip - SimpleDateFormat class overview

 

SimpleDateFormat is a concrete class for the locale sensitive manner to formatting and parsing dates

 

// Date to 1.0 String
SimpleDateFormat sdp = new new SimpleDateFormat ();
String S = sdp.format (new new a Date ());
System.out.println (S); // 2019/8/5 3:14 pm but generally we It does not need Chinese

 

 

 

To 2.0 // Date String
// public String the format Fi NAL (a Date DATE): date datetime string formatted

// The format can be set according to demand ( "yMd")

SimpleDateFormat sdp1 = new SimpleDateFormat ( "y M on MMM d H: m: s"); // or "yyyy, MM-dd day HH: mm: SS"
// Date object as input the output of the current time
System.out .println (sdp1.format (new Date () )); // 2019 year 23 5 February 15:23:37

 

 

 

// String to a Date
String str = "5 May 2019 On May 8 15:35:46";
SimpleDateFormat Formatjx = new new SimpleDateFormat ( "yyyy Year MM month dd day HH: mm: SS");
// public a Date the parse (String source): from the start to the parsed text string to produce a given date
date datejx = Formatjx.parse (str); // throws general analytical will be prompted to try or throws Alt + Enter shortcut keys
System.out.println (datejx );

Guess you like

Origin www.cnblogs.com/ppk-cn/p/11874670.html