java实现时间与字符串之间转换

2025-05-27 0 59

本文实例为大家分享了java实现时间字符串之间转换的具体代码,供大家参考,具体内容如下

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13
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);

}

}

2. 字符串转换成时间

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15
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. 取得当前系统时间,返回yyyy-MM-dd HH:mm:ss字符串

?

1

2

3

4

5

6

7

8

9

10

11

12
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. 取得当前系统时间,返回 HH:mm:ss字符串

?

1

2

3

4

5

6

7

8

9

10

11

12
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.将20101125102503转换成2010-11-25 10:25:03输出

?

1

2

3

4

5

6

7

8

9

10

11

12

13
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);

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

原文链接:http://blog.csdn.net/kiss_the_sun/article/details/6831051

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 java实现时间与字符串之间转换 https://www.kuaiidc.com/77207.html

相关文章

发表评论
暂无评论