Java的。从字符串获取日期和时间,以毫秒为单位

时间:2014-07-08 06:18:44

标签: java date time

我尝试用毫秒来获取字符串的日期和时间:

 SimpleDateFormat formatDate = new SimpleDateFormat("dd.MM.yyyy HH:mm");
 Date modifDate = new Date(Long.parseLong(ftpClient.getModificationTime(file_directory)));
 System.out.println(formatDate.format(modifDate));

但我在第二行有一个例外:java.lang.NumberFormatException: For input string: "213 20140601042221。我的错误在哪里,我该如何解决?

非常感谢!

4 个答案:

答案 0 :(得分:1)

我不确定213是什么,但20140601042221似乎是

YYYYmmddHHMMssSSS

这与你所说的

不符
new SimpleDateFormat("dd.MM.yyyy HH:mm");

请参阅http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

提示:使用parse方法

答案 1 :(得分:1)

FTPClient.getModificationTime()

它返回String表示YYYYMMDDhhmmss格式的最后一个文件修改时间。如果您尝试将其解析为Long而不是不使用该值。

你可以这样做。

try {
    SimpleDateFormat sd=new SimpleDateFormat("YYYYMMDDhhmmss");//From FTPClient
    Date date=sd.parse("20140601042221");//Parse String to date
    SimpleDateFormat sd2=new SimpleDateFormat("dd.MM.yyyy HH:mm");
    System.out.println(sd2.format(date));
} catch (ParseException e) {
    e.printStackTrace();
}

答案 2 :(得分:0)

试试这段代码:

    String dateString=ftpClient.getModificationTime(file_directory));
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
    Date modificationDate = 
        dateFormat.parse(dateString.substring(dateString.indexOf(" ") + 1));
    SimpleDateFormat dateOutputFormat=new SimpleDateFormat("dd.MM.yyyy HH:mm");
    System.out.println(""+dateOutputFormat.format(modificationDate));

答案 3 :(得分:-1)

尝试摆脱213并做一些像:

SimpleDateFormat formatDate = new SimpleDateFormat(“YYYYmmddHHMMssSSS”); 的System.out.println(formatDate.format(ftpClient.getModificationTime(file_directory)));

相关问题