将短美国日期解析为 yyyy-MM-dd,java

发布于 2024-11-07 20:43:23 字数 532 浏览 0 评论 0原文

我想解析日期“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

倾城月光淡如水﹏ 2024-11-14 20:43:23

当我运行它时,它会打印

New date: 2011-03-27

我怀疑您的问题与此无关,而是您的应用程序有一个默认目录,它是 UNC 路径。即您的错误消息所说的正是如此。

尝试从 C: 驱动器或使用网络驱动器号的路径运行此程序。

When I run this it prints

New date: 2011-03-27

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.

一城柳絮吹成雪 2024-11-14 20:43:23
public class date {


    public static void main(String args[])
    {
        String s="03/27/2011";// or 3/27/2011

        SimpleDateFormat dateFormatter=s.length()==9?new SimpleDateFormat("M/dd/yyyy"):new SimpleDateFormat("MM/dd/yyyy");
        try {
            Calendar calendar=Calendar.getInstance();
            Date date=dateFormatter.parse(s);
            calendar.setTime(date);
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
            String str=simpleDateFormat.format(calendar.getTime());
            System.out.println(str);
        } catch (ParseException e) {
              e.printStackTrace();
        }
    }

}

    enter code here
public class date {


    public static void main(String args[])
    {
        String s="03/27/2011";// or 3/27/2011

        SimpleDateFormat dateFormatter=s.length()==9?new SimpleDateFormat("M/dd/yyyy"):new SimpleDateFormat("MM/dd/yyyy");
        try {
            Calendar calendar=Calendar.getInstance();
            Date date=dateFormatter.parse(s);
            calendar.setTime(date);
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
            String str=simpleDateFormat.format(calendar.getTime());
            System.out.println(str);
        } catch (ParseException e) {
              e.printStackTrace();
        }
    }

}

    enter code here
万水千山粽是情ミ 2024-11-14 20:43:23

你也可以这样做

String DATE_FORMAT_NOW = "dd-MM-yyyy HH:mm:ss";

//Instance of the calender class in the utill package
Calendar cal = Calendar.getInstance(); 

//A class that was used to get the date time stamp
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW); 

时间

System.out.println(sdf.format(cal.getTime()) );

打印出你说干杯的

You can also do it like this

String DATE_FORMAT_NOW = "dd-MM-yyyy HH:mm:ss";

//Instance of the calender class in the utill package
Calendar cal = Calendar.getInstance(); 

//A class that was used to get the date time stamp
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW); 

To print out the time you say

System.out.println(sdf.format(cal.getTime()) );

Cheers

浅听莫相离 2024-11-14 20:43:23

我也收到了 AssertionError 抱怨绝对路径,并通过纯粹的暴力找到了解决方法。我想我会在这里发布我的结果,希望正在寻找这个问题答案的人不必像我修复它一样浪费太多时间。

该问题似乎是 Oracle 版本的 Java VM 中的一个错误。如果您首次在非静态对象中创建 java.util.Calendar 对象(直接或间接),则会发生此错误。为了防止这种情况发生,只需在 main() 方法中实例化一个静态的 Calendar 对象即可。随后的非静态实例化将正常工作,至少在我的情况下是这样。用一些简单的东西开始 main()

System.out.println(java.util.Calendar.getInstance().getTime());

就可以了。

希望对某人有帮助。

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 your main() method, which is static. Subsequent non-static instantiations will work ok after that, at least in my case. Starting main() with something simple like

System.out.println(java.util.Calendar.getInstance().getTime());

will do the trick.

Hope that helps someone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文