本文介绍了从FTPClient.getModificationTime()解析日期字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析日期字符串,这是FTP服务器上文件的修改日期。以下是代码。

I am trying to parse a date string which is a modification date of a file on FTP server. Following is the code.

String dateString = mFTPClient.getModificationTime(PDF_FILE_NAME_PS);

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");

Date modificationDate = dateFormat.parse(dateString.substring(dateString.indexOf(" ")));

Log.v(TAG, "inside downloadservice dateString="+dateString);

Log.v(TAG, "inside downloadservice modificationdate="+modificationDate.toString());

我在日志中得到这个

05-27 10:04:20.870: V/DownloadService(751): inside downloadservice dateString=213 20130523130035

05-27 10:04:20.890: V/DownloadService(751): inside downloadservice modificationdate=Sat Jul 23 07:30:35 AEDT 203

任何人都可以请帮我这个?

Can anyone please help me with this?

推荐答案

方法说:
子字符串以指定索引处的字符开头,并延伸到此字符串的末尾。

The javadoc for the String#substring(int index) method says:The substring begins with the character at the specified index and extends to the end of this string.

这就是问题所在有:您没有正确使用 String.substring()方法,因为在调用它时,您会收到另一个 String ,包含一个空格作为第一个字符,这就是解析器的作用istake。

And here is the problem you have: You're not using correctly the String.substring() method, because when invoking it, you receive another String, which contains a space as a first character, which is why the parser does a mistake.

以下是您需要的修复:

String dateString = mFTPClient.getModificationTime(PDF_FILE_NAME_PS);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
Date modificationDate =
     dateFormat.parse(dateString.substring(dateString.indexOf(" ") + 1));

这篇关于从FTPClient.getModificationTime()解析日期字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 10:39