java 时间与字符串之间的转换

1. long字符串转换成yyyy-MM-dd HH:mm:ss格式输出

 1 import java.text.SimpleDateFormat;
 2 import java.util.Date;
 3 //将long字符串转换成格式时间输出
 4 public class LongToString {
 5 public static void main(String argsp[]){
 6     String time="1256006105375";
 7 
 8     Date date=new Date(Long.parseLong(time));
 9     SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
10     time=formatter.format(date);
11     System.out.println(time);
12 }
13 }

2. 字符串转换成时间

 1 import java.text.SimpleDateFormat;
 2 import java.util.Date;
 3 
 4 import ognl.ParseException;
 5 
 6 public class StringToDate {
 7 public static void main(String argsp[]) throws Exception{
 8     String time="2010-11-20 11:10:10";
 9 
10     Date date=null;
11     SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
12     date=formatter.parse(time);
13     System.out.println(date);
14 }
15 }

3. 取得当前系统时间,返回yyyy-MM-dd HH:mm:ss字符串

 1 import java.text.SimpleDateFormat;
 2 import java.util.Date;
 3 
 4 public class StringToDate {
 5 public static void main(String argsp[]) throws Exception{
 6     
 7     Date date=new Date();
 8     SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 9     String time=formatter.format(date);
10     System.out.println(time);
11 }
12 }

4. 取得当前系统时间,返回 HH:mm:ss字符串

 1 import java.text.SimpleDateFormat;
 2 import java.util.Date;
 3 
 4 public class StringToDate {
 5 public static void main(String argsp[]) throws Exception{
 6     
 7     Date date=new Date();
 8     SimpleDateFormat formatter=new SimpleDateFormat("HH:mm:ss");
 9     String time=formatter.format(date);
10     System.out.println(time);
11 }
12 }

5.将20101125102503转换成2010-11-25 10:25:03输出

 1 import java.text.SimpleDateFormat;
 2 import java.util.Date;
 3 
 4 public class StringToDate {
 5 public static void main(String argsp[]) throws Exception{
 6     
 7     String time="20101125102503";
 8     SimpleDateFormat formatter1=new SimpleDateFormat("yyyy-HH-dd HH:mm:ss");
 9     SimpleDateFormat formatter2=new SimpleDateFormat("yyyyHHddHHmmss");
10     time=formatter1.format(formatter2.parse(time));
11     System.out.println(time);
12 }
13 }

 

 

posted @ 2013-05-25 10:30  hlb  阅读(340)  评论(0编辑  收藏  举报