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

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

问题描述

我想要这种格式 2012年12月6日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);


推荐答案

您需要先解析你的日期字符串(使用 DateFormat#parse() 方法)获取 日期 对象使用与日期字符串格式匹配的格式。

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.

然后格式该Date对象(使用 DateFormat#format() 方法)使用 SimpleDateFormat 中所需的格式获取字符串。

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

输出: -

08 Dec 2012 19:09:57

第一种格式的 Z RFC 822 TimeZone ,以匹配日期字符串中的 +0000 。请参阅 SimpleDateFormat 用于您日期格式的各种其他选项。

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天全站免登陆