1. Long string converted to YYYY-MM-DD HH:mm:ss format output
[Java]
- Import Java.text.SimpleDateFormat;
- Import Java.util.Date;
- Converts a long string into a format time output
- public class Longtostring {
- public static void Main (String argsp[]) {
- String time= "1256006105375";
- Date Date=new Date (Long.parselong (time));
- SimpleDateFormat formatter=new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
- Time=formatter.format (date);
- System.out.println (time);
- }
- }
2. String conversion into time
[Java]
- Import Java.text.SimpleDateFormat;
- Import Java.util.Date;
- Import Ognl. ParseException;
- public class Stringtodate {
- public static void Main (String argsp[]) throws exception{
- String time= "2010-11-20 11:10:10";
- Date Date=null;
- SimpleDateFormat formatter=new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
- Date=formatter.parse (time);
- SYSTEM.OUT.PRINTLN (date);
- }
- }
3. Get current system time, return YYYY-MM-DD HH:MM:SS string
[Java]
- Import Java.text.SimpleDateFormat;
- Import Java.util.Date;
- public class Stringtodate {
- public static void Main (String argsp[]) throws exception{
- Date Date=new date ();
- SimpleDateFormat formatter=new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
- String Time=formatter.format (date);
- System.out.println (time);
- }
- }
4. Get current system time, return HH:MM:SS string
[Java]
- Import Java.text.SimpleDateFormat;
- Import Java.util.Date;
- public class Stringtodate {
- public static void Main (String argsp[]) throws exception{
- Date Date=new date ();
- SimpleDateFormat formatter=new SimpleDateFormat ("HH:mm:ss");
- String Time=formatter.format (date);
- System.out.println (time);
- }
- }
5. Convert 20101125102503 to 2010-11-25 10:25:03 output
[Java]
- Import Java.text.SimpleDateFormat;
- Import Java.util.Date;
- public class Stringtodate {
- public static void Main (String argsp[]) throws exception{
- String time= "20101125102503";
- SimpleDateFormat formatter1=new SimpleDateFormat ("Yyyy-hh-dd HH:mm:ss");
- SimpleDateFormat formatter2=new SimpleDateFormat ("Yyyyhhddhhmmss");
- Time=formatter1.format (Formatter2.parse (time));
- System.out.println (time);
- }
- }
Conversion between Java time and string