返回时间戳(以毫秒为单位)

发布于 2024-10-31 15:02:40 字数 1141 浏览 2 评论 0原文

我在将日期和时间转换为毫秒时遇到问题。代码中没有错误,但它返回错误的日期和时间(以毫秒为单位)。

这是我要转换的日期:2011-03-01 17:55:15,它给我这个数字:-679019461843345152

这是我正在使用的代码:

public long getDate(String s)
{
    //this is returning a timestamp but the wrong ones!!!
    String[] formats = new String[]{
            // "yyyy-MM-dd",
            "yyyy-MM-dd HH:mm:ss"
            // "yyyy-MM-dd HH:mmZ",
            //"yyyy-MM-dd HH:mm:ss.SSSZ",
    };

    SimpleDateFormat sdf = null;
    String st;

    for (String format : formats)
    {
        sdf = new SimpleDateFormat(format, Locale.US);
        //System.err.format("%30s %s\n", format, sdf.format(new Date(0)));

        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        st = new String(sdf.format(new Date(0)));
        System.err.format(format, st);
    }

    // compute nanoseconds from y, m...

    //return that number 
    Calendar c = Calendar.getInstance();
    c.set(sdf.YEAR_FIELD, sdf.MONTH_FIELD, sdf.DATE_FIELD, sdf.HOUR0_FIELD, sdf.MINUTE_FIELD, sdf.SECOND_FIELD);
    return c.getTimeInMillis() * 1000000;
}

I am having a problem with converting the date and time to milliseconds. There are no errors in the code but it's returning the wrong date and time in milliseconds.

This is the date that I'm converting: 2011-03-01 17:55:15 and it gives me this number: -679019461843345152.

This is the code I'm using:

public long getDate(String s)
{
    //this is returning a timestamp but the wrong ones!!!
    String[] formats = new String[]{
            // "yyyy-MM-dd",
            "yyyy-MM-dd HH:mm:ss"
            // "yyyy-MM-dd HH:mmZ",
            //"yyyy-MM-dd HH:mm:ss.SSSZ",
    };

    SimpleDateFormat sdf = null;
    String st;

    for (String format : formats)
    {
        sdf = new SimpleDateFormat(format, Locale.US);
        //System.err.format("%30s %s\n", format, sdf.format(new Date(0)));

        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        st = new String(sdf.format(new Date(0)));
        System.err.format(format, st);
    }

    // compute nanoseconds from y, m...

    //return that number 
    Calendar c = Calendar.getInstance();
    c.set(sdf.YEAR_FIELD, sdf.MONTH_FIELD, sdf.DATE_FIELD, sdf.HOUR0_FIELD, sdf.MINUTE_FIELD, sdf.SECOND_FIELD);
    return c.getTimeInMillis() * 1000000;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

绝影如岚 2024-11-07 15:02:40

c.set 行没有任何意义:

c.set(sdf.YEAR_FIELD, sdf.MONTH_FIELD, sdf.DATE_FIELD, sdf.HOUR0_FIELD, sdf.MINUTE_FIELD, sdf.SECOND_FIELD);

这应该给您一个想法:

       Calendar c = Calendar.getInstance();
       try {
           dt = sdf.parse("2011-03-01 17:55:15"); 
       } catch (ParseException e) {
           System.err.println("There's an error in the Date!");
           return null;
       }   
       Date dt = sdf.parse("2011-03-01 17:55:15");   
       c.setTime(dt);
       System.out.println( c.getTimeInMillis() * 1000000);   
       System.out.println(dt.toString());   

输出:

1299002115000000000
Tue Mar 01 12:55:15 EST 2011

顺便说​​一句,您永远不会访问参数 s。

The line c.set does not make any sense:

c.set(sdf.YEAR_FIELD, sdf.MONTH_FIELD, sdf.DATE_FIELD, sdf.HOUR0_FIELD, sdf.MINUTE_FIELD, sdf.SECOND_FIELD);

This should give you an idea:

       Calendar c = Calendar.getInstance();
       try {
           dt = sdf.parse("2011-03-01 17:55:15"); 
       } catch (ParseException e) {
           System.err.println("There's an error in the Date!");
           return null;
       }   
       Date dt = sdf.parse("2011-03-01 17:55:15");   
       c.setTime(dt);
       System.out.println( c.getTimeInMillis() * 1000000);   
       System.out.println(dt.toString());   

outputs:

1299002115000000000
Tue Mar 01 12:55:15 EST 2011

BTW, you are never accessing the parameter s.

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