0

please, i have a problem in my code, i have a method that gives me a calendar but it's a string:

lastModif = sftpChannel.lstat(remoteFile).getMtimeString();

the output is String :

example:

System.out.println(lastModif); || output is ==>  Tue Oct 14 12:48:15 WEST 2014

I want to format this string to have just this outpu : 2014-10-14.

I don't know how can i convert this String "Oct 14 12:48:15 WEST 2014" to date.

and after this conversion i will compare two date.

Thank you

mond14
  • 139
  • 1
  • 8
  • I don't either. What timezone is WEST? – Elliott Frisch Oct 14 '14 at 16:09
  • SimpleDateFormat http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html – user1717259 Oct 14 '14 at 16:10
  • @ElliottFrisch: [West European Summer Time](http://www.timeanddate.com/library/abbreviations/timezones/eu/west.html) – Makoto Oct 14 '14 at 16:16
  • @mond14 What *exactly* is the data type of `lastModif`? – Basil Bourque Oct 14 '14 at 16:20
  • possible duplicate of [Java string to date conversion](http://stackoverflow.com/questions/4216745/java-string-to-date-conversion) – Basil Bourque Oct 14 '14 at 16:22
  • According to the Javadoc, [`getMtimeString()`](http://epaul.github.io/jsch-documentation/simple.javadoc/com/jcraft/jsch/SftpATTRS.html#getMtimeString()) returns a String representation of the modification time, and [`getMTime()`](http://epaul.github.io/jsch-documentation/simple.javadoc/com/jcraft/jsch/SftpATTRS.html#getMTime()) returns the number of seconds since January 1, 1970, as an `int`. – David Conrad Oct 14 '14 at 16:25

3 Answers3

0
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");      
Date date = new Date();     // Or any other date.

System.out.println(sdf.format(date));

And the output will be 2014-10-14

Mohammad Najar
  • 2,009
  • 2
  • 21
  • 31
0

You can use a couple of SimpleDateFormats - one to parse the string you got, and one to reformat it:

String lastModif = sftpChannel.lstat(remoteFile).getMtimeString();
DateFormat parser = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy");
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

System.out.println(formatter.format(parser.parse(lastModif)));
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

According to the Javadoc, getMtimeString() returns a String representation of the modification time, and getMTime() returns the number of seconds since January 1, 1970 (the epoch), as an int.

Java typically takes a date as either some string to be parsed, or a long number of milliseconds since the epoch, or, in the case of java.time.LocalDate, it can take the number of days since the epoch.

Since getMTime() returns the number of seconds since the epoch, and there are 86400 seconds in a day, you can simply use it instead of getMtimeString() and create a LocalDate from it:

import static java.time.format.DateTimeFormatter.ISO_LOCAL_DATE;

import java.time.LocalDate;

public class DaysToDate {
    public static void main(String[] args) {
        // ... whatever you need to do to get sftpChannel ...
        int lastModif = sftpChannel.lstat(remoteFile).getMTime();
        long days = lastModif / 86400L;
        LocalDate date = LocalDate.ofEpochDay(days);

        System.out.println(date.format(ISO_LOCAL_DATE));
        System.out.println(date);
    }
}

I have used a DateTimeFormatter to format the date (there is a predefined formatter for the format you specified named ISO_LOCAL_TIME), but that is the same as the default format for LocalDate, so you can just call its toString() method. If you want to format it differently, just create a DateTimeFormatter with the desired format.

Note: Since getMTime() represents the number of seconds since January 1, 1970 as a 32-bit int, it suffers from the 2038 problem.

David Conrad
  • 15,432
  • 2
  • 42
  • 54