两个以字符串形式存储的时间之间的差异

huangapple go评论147阅读模式
英文:

Difference between two times stored as Strings

问题

我有两个形如“dd-MM-YYYY HH:mm:ss”的字符串。如何找出这两个字符串之间的小时差异?

英文:

I have two strings of the form "dd-MM-YYYY HH:mm:ss". How can I find the difference in hours between the two Strings ?

答案1

得分: 1

你可以将 String 转换为 Date 对象,然后进行比较。类似以下方式:

SimpleDateFormat sdformat = new SimpleDateFormat("dd-MM-YYYY HH:mm:ss");
Date d1 = sdformat.parse("04-05-2020 02:33:12");
Date d2 = sdformat.parse("05-05-2020 02:33:12");
System.out.println("The date 1 is: " + sdformat.format(d1));
System.out.println("The date 2 is: " + sdformat.format(d2));
if(d1.compareTo(d2) > 0) {
    System.out.println("Date 1 occurs after Date 2");
} else if(d1.compareTo(d2) < 0) {
    System.out.println("Date 1 occurs before Date 2");
} else if(d1.compareTo(d2) == 0) {
    System.out.println("Both dates are equal");
}
英文:

You can converted the String into Date objects, then you may compare them.
Something like below

 SimpleDateFormat sdformat = new SimpleDateFormat(&quot;dd-MM-YYYY HH:mm:ss&quot;);
  Date d1 = sdformat.parse(&quot;04-05-2020 02:33:12&quot;);
  Date d2 = sdformat.parse(&quot;05-05-2020 02:33:12&quot;);
  System.out.println(&quot;The date 1 is: &quot; + sdformat.format(d1));
  System.out.println(&quot;The date 2 is: &quot; + sdformat.format(d2));
  if(d1.compareTo(d2) &gt; 0) {
     System.out.println(&quot;Date 1 occurs after Date 2&quot;);
  } else if(d1.compareTo(d2) &lt; 0) {
     System.out.println(&quot;Date 1 occurs before Date 2&quot;);
  } else if(d1.compareTo(d2) == 0) {
     System.out.println(&quot;Both dates are equal&quot;);
  }

答案2

得分: 0

使用SimpleDateFormatDate,你可以这样做:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public static long date_hour_diff(String start_date, String end_date) {
  SimpleDateFormat simple_date_format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
  try {
    Date parsed_start_date = simple_date_format.parse(start_date);
    Date parsed_end_date = simple_date_format.parse(end_date);

    long time_diff = parsed_end_date.getTime() - parsed_start_date.getTime();
    long diff_hours = (time_diff / (1000 * 60 * 60)) % 24;
    return diff_hours;
  } catch (ParseException e) {
    Log.e(TAG, "Date is not valid.");
  }
}
英文:

You can do it using SimpleDateFormat and Date:

import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 


public static long date_hour_diff(String start_date, String end_date) { 
  SimpleDateFormat simple_date_format = new SimpleDateFormat(&quot;dd-MM-yyyy HH:mm:ss&quot;); 
  try {  
    Date parsed_start_date = simple_date_format.parse(start_date); 
    Date parsed_end_date = simple_date_format.parse(end_date); 
  
    long time_diff = d2.getTime() - d1.getTime(); 
    long diff_hours = (time_diff / (1000 * 60 * 60)) % 24; 
    return diff_hours;
  } catch (ParseException e) { 
      Log.e(TAG, &quot;Date is not valid.&quot;);
  } 
}

huangapple
  • 本文由 发表于 2020年8月18日 19:02:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63467232.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定