How to convert string quot;2011-11-29 12:34:25quot; to date in quot;dd-MM-yyyyquot; format in JAVA(如何转换字符串“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);
推荐答案
首先,您必须将日期时间的字符串表示形式解析为 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 对象格式化回您喜欢的格式的字符串.
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格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何转换字符串“2011-11-29 12:34:25"迄今为止在“dd-MM-yyyy"中JAVA格式
基础教程推荐
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 降序排序:Java Map 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01