将短美国日期解析为 yyyy-MM-dd,java
我想解析日期“3/27/11”,我认为它等于美国短日期。
DateFormat df1 = new SimpleDateFormat("MM/dd/yy");
DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");
Date date = (Date) df1.parseObject("03/27/11");
System.out.println("New date: " + df2.format(date));
我在几个java教程中找到了上面的代码,但它似乎不起作用。 对于一些我如何得到的,
Exception in thread "main" java.lang.AssertionError: Default directory must be absolute/non-UNC
这是我想要实现的,
输入:3/27/11
(03/27/11 也应该是有效的输入)
输出:2011-03-27
提前致谢!
I want to parse the date "3/27/11" which I think is equal to US short date.
DateFormat df1 = new SimpleDateFormat("MM/dd/yy");
DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");
Date date = (Date) df1.parseObject("03/27/11");
System.out.println("New date: " + df2.format(date));
I found the code above in several java tutorials but it doesn't seem to work.
For some how I get,
Exception in thread "main" java.lang.AssertionError: Default directory must be absolute/non-UNC
Here is what I want to achieve,
input: 3/27/11
(03/27/11 should also be a valid input)
output: 2011-03-27
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当我运行它时,它会打印
我怀疑您的问题与此无关,而是您的应用程序有一个默认目录,它是 UNC 路径。即您的错误消息所说的正是如此。
尝试从 C: 驱动器或使用网络驱动器号的路径运行此程序。
When I run this it prints
I suspect your problem is nothing to do with this but rather you have a default directory for your application which is a UNC path. i.e. exactly what your error message says.
Try running this program from your C: drive or a path using a network drive letter.
你也可以这样做
时间
打印出你说干杯的
You can also do it like this
To print out the time you say
Cheers
我也收到了
AssertionError
抱怨绝对路径,并通过纯粹的暴力找到了解决方法。我想我会在这里发布我的结果,希望正在寻找这个问题答案的人不必像我修复它一样浪费太多时间。该问题似乎是 Oracle 版本的 Java VM 中的一个错误。如果您首次在非静态对象中创建 java.util.Calendar 对象(直接或间接),则会发生此错误。为了防止这种情况发生,只需在
main()
方法中实例化一个静态的 Calendar 对象即可。随后的非静态实例化将正常工作,至少在我的情况下是这样。用一些简单的东西开始 main()就可以了。
希望对某人有帮助。
I was getting the
AssertionError
complaining about the absolute path as well, and found a workaround through sheer brute force. I thought I would post my results here with the hopes that someone that is searching for an answer to this problem will not have to waste as much time as I did fixing it.The problem seems to be a bug in Oracle's version of the Java VM. If you create a
java.util.Calendar
object (either directly or indirectly) for the first time in a non-static object, then this error occurs. To prevent it, just instantiate a Calendar object in yourmain()
method, which is static. Subsequent non-static instantiations will work ok after that, at least in my case. Starting main() with something simple likewill do the trick.
Hope that helps someone.