String-Date转换为纳秒

Rha*_*ooz 15 java android datetime-format date-parsing simpledateformat

我已经用这段代码为Android应用程序挣扎了一段时间,我无法理解它.我已经阅读并尝试了我在stackoverflow和其他地方找到的每个解决方案,但仍然没有运气.

我想要做的是有一个函数将字符串转换为"17.08.2012 05:35:19:7600000"UTC日期和一个函数,该函数接受UTC date并将其转换为类似的字符串.

String value = "17.08.2012 05:35:19:7600000";
DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss:SSSSSSS");
try
{
  Date today = df.parse(value);
  System.out.println("Today = " + df.format(today) + " " + today.toGMTString());
} 
catch (ParseException e)
{
  e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

这导致:Today = 17.08.2012 07:41:59:0000000 17 Aug 2012 04:41:59 GMT哪都是错误的.

我试着SDF's timezoneUTC,没有运气.
我注意到的另一件事:如果我这样做,df.setLenient(false);
它会让我:java.text.ParseException: Unparseable date: "17.08.2012 05:35:19:7600000".

如果有人能提供一些解释/示例代码,我将非常感激.提前致谢

Eri*_*ric 22

你得到的结果是绝对正确的.

我们来分析一下:

17.08.2012 05:35:19:7600000
Run Code Online (Sandbox Code Playgroud)
  • 17:月份(17日)
  • 08:一年中的月份(八月)
  • 2012年:年(2012年)
  • 05:一天中的小时(早上5点)
  • 35:一小时(:35)
  • 19:分秒(:19)
  • 7600000:毫秒秒(7,600,000)

现在,VM看到这种情况的方式是你将时间宣布为5:35:19 am,然后再添加7,600,000毫秒.7,600,000毫秒= 7,600秒= 2小时6分40秒.凌晨5:35:19 + 02:06:40 = 7:41:59 am(和0毫秒)这是你得到的结果.(您似乎没有正确设置时区,因此GMT字符串比您的结果落后3小时.)

如果你想保留:7600000,据我所知这是不可能的.由于这可以简化为几秒钟,因此VM会自动将其减少到其他时间增量.毫秒(the SSSS)应该用于存储值<1000.

我建议你SimpleDateFormat为你的输出创建一个新的; 但请注意,将毫秒被吸收到其他时间(因为它们都作为单个存储longDate对象).


Raj*_*ram 6

    private String convertDate(String cdate)
{
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss:SSSSSSS");
    SimpleDateFormat postFormater = new SimpleDateFormat("yyyy-MM-dd");
    Date convertedDate;
    try
    {
        convertedDate = dateFormat.parse(cdate);
        cdate = postFormater.format(convertedDate);
    }
    catch (ParseException e)
    {
        Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_SHORT).show();
    }
    return cdate;
}
Run Code Online (Sandbox Code Playgroud)

试试这个.


sla*_*dan 6

这就是您所需要的(但它会丢失毫秒信息):

"dd.MM.yyyy HH:mm:ss.'000000'"
Run Code Online (Sandbox Code Playgroud)

如果您使用"dd.MM.yyyy HH:mm:ss.SSSSSS", 那么您的毫秒数将得到三个前导零。

如果您使用"dd.MM.yyyy HH:mm:ss.SSS'000'",那么您可以格式化日期,但不能解析任何日期。

试试看:

public static void main(String[] args) throws ParseException {
    printDate("dd.MM.yyyy HH:mm:ss.SSS");//02.05.2010 21:45:58.073
    printDate("dd.MM.yyyy HH:mm:ss.SSSSSS");//02.05.2010 21:45:58.000073
    printDate("dd.MM.yyyy HH:mm:ss.SSS'000'");//02.05.2010 21:45:58.073000
    printDate("dd.MM.yyyy HH:mm:ss.'000000'");//02.05.2010 21:45:58.000000

    tryToParseDate("dd.MM.yyyy HH:mm:ss.SSS");//good
    tryToParseDate("dd.MM.yyyy HH:mm:ss.SSSSSS");//good
    tryToParseDate("dd.MM.yyyy HH:mm:ss.SSS'000'");//bad
    tryToParseDate("dd.MM.yyyy HH:mm:ss.'000000'");//good
}

private static void printDate(String formatString) {
    Date now = new Date();
    SimpleDateFormat format = new SimpleDateFormat(formatString);
    String formattedDate = format.format(now);

    // print that date
    System.out.println(formattedDate);
}

private static void tryToParseDate(String formatString) {
    Date now = new Date();
    SimpleDateFormat format = new SimpleDateFormat(formatString);
    String formattedDate = format.format(now);

    // try to parse it again
    try {
        format.parse(formattedDate);
        System.out.println("good");
    } catch (ParseException e) {
        System.out.println("bad");
    }
}
Run Code Online (Sandbox Code Playgroud)