0

I am developing an application which will check the last modified time of a file from google drive and last modified time of file on local machine, and compare them, if file on the local machine is found to be latest modified then it will be uploaded or else file from cloud will be uploaded. I have upload down load code. I have file last modified time of the local file and also of the google drive file but the problem is the DateTime which is returned from google drive on calling this function lastModTimeCloudFile = f.getModifiedByMeDate(); it returns DateTime which is of Google api. Now I want to convert it to java.util.Date format how can I do it. Please help me all the experts out their. Here's what I have tried.

   for (File f : result) {
                    String ext = null;
                    fileName = f.getTitle();
                    if (fileName.equals("IDPWD")) {
                        nFlagIfFile = 1;
                        lastModTimeLocalFile = new Date(dbFile.lastModified());
                        localModTime = formatter.format(lastModTimeLocalFile);
                        java.util.Date date ;
                        lastModTimeCloudFile = f.getModifiedByMeDate();
                        try {
                             date = formatter.parse(lastModTimeCloudFile.toString());
                        } catch (ParseException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }


                    }

                }

This gives error

java.text.ParseException: Unparseable date: "2013-12-27T11:15:10.382Z"
    at java.text.DateFormat.parse(Unknown Source)
    at GetNewFile.main(GetNewFile.java:89)

How can I do it. Please point me to right direction.

Thanks in advance.

Mayur
  • 789
  • 10
  • 37

2 Answers2

4

There is an error in your formatter which you haven't shown.

You need to parse the String "2013-12-27T11:15:10.382Z", here is a good example:

https://stackoverflow.com/a/8405125/3115739

Try this code:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
format.setTimeZone(TimeZone.getTimeZone("UTC"));

String dateString = "2013-12-27T11:15:10.382Z";

try {
    Date date  = format.parse(dateString);
    System.out.println(date);
}
catch (Exception e) {
    e.printStackTrace();
}
Community
  • 1
  • 1
nillas
  • 431
  • 3
  • 4
  • My formatter looks something like this `DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh-MM-ss");` And the folder last modified date which I get is in long format which is then converted to DateTime using the formatter. – Mayur Dec 28 '13 at 04:06
0

How does you formatter look like ? Check for the time format that gets printed. Its a timestamp with T and Z. Google it...

OR.

Convert to milliseconds and use the Calendar Object

sanket
  • 789
  • 4
  • 16