Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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());

I get this in my log

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
187 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.

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.

Here's the fix you need:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...