Conversion between java time and string

Source: Internet
Author: User

Conversion between java time and string

1. convert long string to yyyy-MM-dd HH: mm: ss format output

 

Import java. text. simpleDateFormat; import java. util. date; // convert the long String to the 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 to 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";Date date=null;SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");date=formatter.parse(time);System.out.println(date);}}

3. Obtain the current system time and return the string yyyy-MM-dd HH: mm: ss.

 

 

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 the current system time and return the 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 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);}}



 

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.