java字符串和时间转换
import java.text.SimpleDateFormat; import java.util.Date; //将long字符串转换成格式时间输出 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); } }
//字符串转换成时间 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); }
//取得当前系统时间,返回yyyy-MM-dd HH:mm:ss字符串 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); } }
//取得当前系统时间,返回 HH:mm:ss字符串 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); } }
//将20101125102503转换成2010-11-25 10:25:03输出 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); } }