如何转换字符串“2011-11-29 12:34:25”迄今为止,“dd-MM-yyyy”格式在JAVA中 [英] How to convert string "2011-11-29 12:34:25" to date in "dd-MM-yyyy" format in JAVA

查看:118
本文介绍了如何转换字符串“2011-11-29 12:34:25”迄今为止,“dd-MM-yyyy”格式在JAVA中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试转换字符串中的日期,格式为yyyy-MM-dd HH:mm:ss到dd-MM-yyyy。

I am trying to convert date which is in string and got format of "yyyy-MM-dd HH:mm:ss" to "dd-MM-yyyy".

我已经隐含了以下代码,但它的赋值:java.lang.IllegalArgumentException

I have implmented following code but its giving : java.lang.IllegalArgumentException

        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        Date date = new Date(values);
        String mydate = dateFormat.format(date);


推荐答案

首先你必须解析日期的字符串表示日期对象的时间。

First you have to parse the string representation of your date-time into a Date object.

DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = (Date)formatter.parse("2011-11-29 12:34:25");

然后,将Date对象格式化为首选格式的String。

Then you format the Date object back into a String in your preferred format.

DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
String mydate = dateFormat.format(date);

这篇关于如何转换字符串“2011-11-29 12:34:25”迄今为止,“dd-MM-yyyy”格式在JAVA中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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