Conversion between Java time and string

Source: Internet
Author: User

1. Long string converted to YYYY-MM-DD HH:mm:ss format output

Import java.text.simpledateformat;import java.util.date;//Convert long string to format time output public class Longtostring {public static void main (String argsp[]) {string time= "1256006105375";D ate 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

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 ";D ate date=null; SimpleDateFormat formatter=new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");d Ate=formatter.parse (time); SYSTEM.OUT.PRINTLN (date);}}

3. Get current system time, return YYYY-MM-DD HH:mm:ss string

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

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

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

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.