java - Java - DateFormat 在转换日期时不起作用
问题描述
我是 Java 新手。我一直在尝试将日期转换为dd-MMM-yy格式。但我得到了例外:
Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Date
下面是我的代码。请指导。
public class Test {
public static void main(String args[ ]) {
String currentDateString =new String();
DateFormat dateFormat = new SimpleDateFormat("MMM dd, yyyy");
DateFormat dateFormatpdfname = new SimpleDateFormat("dd-MMM-yy");
//Date currentDate = new Date();
String dateInString = "Sep 16, 2018";
String dateInString1 = "16-Sep-18";
String currentDateVal=dateFormatpdfname.format(dateInString1);
currentDateString = dateFormat.format(dateInString);
System.out.println(currentDateVal);
System.out.println(currentDateString);
}
}
解决方案
Uncomment this
//Date currentDate = new Date();
Then,
String currentDateVal=dateFormatpdfname.format(currentDate );
currentDateString = dateFormat.format(currentDate );
推荐阅读
- python - 使用单词作为另一个文本文件中的键从文本文件中提取行
- python - selenium.common.exceptions.WebDriverException:消息:“Mozilla Firefox”可执行文件在使用 GeckoDriver 时可能具有错误的权限
- php - 如何使用 MySQL 函数按属性删除 JSON 数组中的对象?
- ios - 使用 DJI UXSDKDemo (iOS) 记录数据的最佳方法
- php - 如何创建一个 cron 作业
- php - PHP 两个不同的按钮发送相同的数据
- html - VBA填写不是文本区域的在线表格
- datetime - 特定日期时间的 forfiles
- lua - 如何在 Lua 中设置定义的十进制值?
- django - 如何实时检查 Django 表单中的输入部分