时间差 - 从午夜之前到午夜之后没有约会

时间:2018-04-12 08:24:56

标签: java time

我正在努力计算午夜之后的时间:

 final Task<DriveFolder> appfolder = getDriveResourceClient().getAppFolder();
    appfolder.addOnSuccessListener(new OnSuccessListener<DriveFolder>() {
      @Override
      public void onSuccess(DriveFolder driveFolder) {
        listFilesInFolder(driveFolder);
      }
    }).addOnFailureListener(new OnFailureListener() {
      @Override
      public void onFailure(@NonNull Exception e) {

      }
    });

private void listFilesInFolder(final DriveFolder folder) {
    Query query = new Query.Builder()
            .addFilter(Filters.eq(SearchableField.TITLE, "File.txt"))
            .build();
    // [START query_children]
    //Task<MetadataBuffer> queryTask = getDriveResourceClient().queryChildren(folder, query);
    Task<MetadataBuffer> queryTask = getDriveResourceClient().listChildren(folder);
    // END query_children]
    queryTask
            .addOnSuccessListener(this,
                    new OnSuccessListener<MetadataBuffer>() {
                        @Override
                        public void onSuccess(MetadataBuffer metadataBuffer) {

                          for (int i=0; i<metadataBuffer.getCount(); i++){
                            Metadata mb = metadataBuffer.get(0);
                            Log.e("File name", mb.getOriginalFilename());
                          }
                            if (metadataBuffer.getCount() > 0) {
                                Metadata mb = metadataBuffer.get(0);

                            retrieveContents(mb.getDriveId().asDriveFile());


                            } else {

                              listFilesInFolder(folder);


                              showMessage("There are no File Found.");

                            }

                        }
                    })
            .addOnFailureListener(this, new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.e(TAG, "Error retrieving files", e);
                    showMessage(getString(R.string.query_failed));
                    finish();
                }
            });

}

1 个答案:

答案 0 :(得分:3)

如果您不关心夏令时变化,并且您认为世界是理想的(它不是),您可以减去结束和开始之间的持续时间(将end视为从24小时开始和start结束):

String time = "22:00-01:05";
String[] parts = time.split("-");

LocalTime start = LocalTime.parse(parts[0]);
LocalTime end = LocalTime.parse(parts[1]);
if (start.isBefore(end)) { // normal case
    System.out.println(Duration.between(start, end));
} else { // 24 - duration between end and start, note how end and start switched places
    System.out.println(Duration.ofHours(24).minus(Duration.between(end, start)));
}