parsing a date string from FTPClient.getModificationTime()

非 Y 不嫁゛ 提交于 2019-11-27 19:33:29

问题


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?


回答1:


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



回答2:


tl;dr

LocalDateTime.parse(
    "20130523130035" ,
    DateTimeFormatter.ofPattern( "uuuuMMddHHmmss" )
).toString()

2013-05-23T13:00:35

java.time

The modern approach uses java.time classes.

Your input lacks an indicator of offset-from-UTC or time zone. So we parse as a LocalDateTime. As such, this is not a moment, not a specific point on the timeline, but only a idea of potential moments along a range of about 26-27 hours on the timeline.

String input = "213 20130523130035".split( " " )[1] ;  // Grab second piece of text from your input string, the 20130523130035 part.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuuMMddHHmmss" ) ;
LocalDateTime ldt = LocalDateTime.parse( input , f ) ;

ldt.toString(): 2013-05-23T13:00:35


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Using a JDBC driver compliant with JDBC 4.2 or later, you may exchange java.time objects directly with your database. No need for strings nor java.sql.* classes.

Where to obtain the java.time classes?

  • Java SE 8, Java SE 9, and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android bundle implementations of the java.time classes.
    • For earlier Android, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.



来源:https://stackoverflow.com/questions/16764893/parsing-a-date-string-from-ftpclient-getmodificationtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!