-3

I try to get date and time from string with milliseconds:

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

But I have an exception in second line: java.lang.NumberFormatException: For input string: "213 20140601042221. Where is my mistake and how can I solve it?

Thank you very much!

user3649515
  • 199
  • 7
  • 17
  • 1
    How are you expecting `"213 20140601042221` to be parsed as a long? – David Ehrmann Jul 08 '14 at 06:22
  • I am guessing your `String` is not a `Long`. If your `String` is `213 20140601042221` should be split to produce 2 `Long`s. – Eypros Jul 08 '14 at 06:23
  • Any chance this answers your question? [parsing a date string from FTPClient.getModificationTime()](https://stackoverflow.com/questions/16764893/parsing-a-date-string-from-ftpclient-getmodificationtime) – David Ehrmann Jul 08 '14 at 06:23
  • `21320140601042221` is The value you want or May be just `20140601042221` use `split(" ")[1]` in `Long.parseLong` – akash Jul 08 '14 at 06:24

4 Answers4

1

well I am not sure what 213 is but 20140601042221 seems to be

YYYYmmddHHMMssSSS

this does not match what you have stated as

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

see http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Hint: use the parse method

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
1

FTPClient.getModificationTime()

It returns String representing the last file modification time in YYYYMMDDhhmmss format.So if you are trying to parse it to Long than no use of that value.

You can do it like this.

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();
}
akash
  • 22,664
  • 11
  • 59
  • 87
0

Try this code :

    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));
user3717646
  • 436
  • 3
  • 10
-1

Try getting rid of the 213 and do something like:

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

Eric
  • 11
  • 1