字符串转换成日期格式 [英] convert string into date format

查看:196
本文介绍了字符串转换成日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这个格式 2012 12月6日12:10

 字符串时间为2012年12月8日13点39分57秒+0000;

  日期格式SDF =新的SimpleDateFormat(HH:MM:SS);
  日期日期= sdf.parse(时间);

  的System.out.println(时间:+日期);
 

解决方案

您需要先解析您的日期字符串(使用<一个href="http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html#parse%28java.lang.String%29"><$c$c>DateFormat#parse()方法),以获取 日期 对象使用的日期字符串的格式相匹配的格式

和和格式的Date对象(使用<一个href="http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html#format%28java.util.Date%29"><$c$c>DateFormat#format()法)使用所需的格式的SimpleDateFormat 得到的字符串。

 字符串时间为2012年12月8日13点39分57秒+0000;

日期日期=新的SimpleDateFormat(YYYY-MM-DD HH:MM:SS Z)解析(时间)。
字符串str =新的SimpleDateFormat(DD MMM YYYY HH:MM:SS)格式(日期)。

的System.out.println(STR);
 

输出: -

  2012年12月8日19时09分57秒
 

以Z 第一格式是的 RFC 822时区匹配 +0000 在你的日期字符串。请参阅<一href="http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html"><$c$c>SimpleDateFormat为在你的日期格式可用各种其它选项。

I want this format 6 Dec 2012 12:10

  String time = "2012-12-08 13:39:57 +0000 ";

  DateFormat sdf = new SimpleDateFormat("hh:mm:ss");
  Date date = sdf.parse(time);

  System.out.println("Time: " + date);

解决方案

You need to first parse your date string (Use DateFormat#parse() method) to get the Date object using a format that matches the format of date string.

And then format that Date object (Use DateFormat#format() method) using the required format in SimpleDateFormat to get string.

String time = "2012-12-08 13:39:57 +0000";

Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z").parse(time);
String str = new SimpleDateFormat("dd MMM yyyy HH:mm:ss").format(date);

System.out.println(str);

Output: -

08 Dec 2012 19:09:57

Z in the first format is for RFC 822 TimeZone to match +0000 in your date string. See SimpleDateFormat for various other options to be used in your date format.

这篇关于字符串转换成日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆