This example shares the specific code for Java to implement the conversion between time and strings for your reference. The specific content is as follows
1. Convert long string 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"; 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. Convert string 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. Get the current system time and return the 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 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 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); } }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.