在我的程序中,我必须显示CountDown计时器,为此我写了一些代码,这使我可以获得天,小时,分钟和秒,但不知道如何计算年,月和周.
我仍然得到这个:
days:384 hours:6 minutes:27 seconds:25
Run Code Online (Sandbox Code Playgroud)
但我需要这个:
year:1 months:2 weeks:5 days:125 hours:6 minutes:27 seconds:25
Run Code Online (Sandbox Code Playgroud)
检查我的以下代码:
public class MainActivity extends Activity {
String time1="31-08-2015 12:01:00";
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
tv=(TextView)findViewById(R.id.textView1);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Calendar start = Calendar.getInstance();
String time = "dd-MM-yyyy hh:mm:ss"; // 12:00
String dateStart =(String) DateFormat.format(time, start);
String dateStop = time1;
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
// Custom date format
java.util.Date d1=null;
java.util.Date d2=null;
try {
d1 = format.parse(dateStart);
d2 = format.parse(dateStop);
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long diff = d2.getTime() - d1.getTime();
long diffdays =(diff/ (1000*60*60*24));
long diffHours = ((diff - (1000*60*60*24*diffdays)) / (1000*60*60));
long diffMinutes = (diff - (1000*60*60*24*diffdays) - (1000*60*60*diffHours)) / (1000*60);
long diffSeconds =(diff - (1000*60*60*24*diffdays) - (1000*60*60*diffHours) -(1000*60*diffMinutes))/ (1000);
String out= "days:"+diffdays+ "hours:"+diffHours+ "Minutes:"+diffMinutes+ "seconds:"+diffSeconds;
tv.setText(out);
handler.postDelayed(this, 1000);
}
}, 1000);
}
}
Run Code Online (Sandbox Code Playgroud)
我有这段适合我的代码,而且更简单:
public static String dhmsDifference(Date d1, Date d2)
{
long diff = d2.getTime() - d1.getTime();
long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000) % 24;
long diffDays = diff / (24 * 60 * 60 * 1000);
String difference=Long.toString(diffDays)+":"+String.format("%02d", diffHours)+":"+String.format("%02d", diffMinutes)+":"+String.format("%02d", diffSeconds);
return difference;
}
Run Code Online (Sandbox Code Playgroud)
它返回一个字符串,但我认为您可以轻松地将其适应您的代码。
对于月份和年份,我会使用日历(将其添加到我的代码中):
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(d1);
cal2.setTime(d2);
long months = cal2.get(Calendar.MONTH)-cal1.get(Calendar.MONTH);
long years = cal2.get(Calendar.YEAR)-cal1.get(Calendar.YEAR);
long weeks = cal2.get(Calendar.WEEK_OF_YEAR)-cal1.get(Calendar.WEEK_OF_YEAR);
Run Code Online (Sandbox Code Playgroud)
当年份发生变化时,您需要添加一些魔法,但这就是想法。
编辑:请使用这个更完整的代码进行一些测试:
public static String ymwdhmsDifference(Date d1, Date d2)
{
long diff = d2.getTime() - d1.getTime();
long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000) % 24;
long totalDiffDays = diff / (24 * 60 * 60 * 1000);
long diffDays = totalDiffDays % 7;
long diffWeeks = totalDiffDays/7; // Full weeks are simply days / 7.
diffDays = diffDays % 7; // now we get the remaining days.
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(d1);
cal2.setTime(d2);
long totalDiffYears = cal2.get(Calendar.YEAR)-cal1.get(Calendar.YEAR);
long totalDiffMonths = Math.max(totalDiffYears*12+cal2.get(Calendar.MONTH)-cal1.get(Calendar.MONTH)-1,0);
long diffYears = totalDiffMonths / 12;
// remaining full months
long diffMonths = totalDiffMonths % 12;
// now we have to count how many weeks those full months represent, to substract them from the number of weeks...
Calendar cal3 = Calendar.getInstance();
cal3.setTime(d1);
int month = cal1.get(Calendar.MONTH)+1, year = cal1.get(Calendar.YEAR), monthDays=0;
for (int m=0;m<totalDiffMonths;m++) {
cal3.set(Calendar.MONTH, month++);
monthDays+=cal3.getActualMaximum(Calendar.DAY_OF_MONTH);
if (month>=12) {
month = 0;
year++;
cal3.set(Calendar.YEAR, year);
}
}
diffWeeks-=monthDays/7;
// Note that the number of weeks can be greater than 4 because they are part of non full months.
String difference="Y: "+Long.toString(diffYears)+" M: "+Long.toString(diffMonths)+" W: "+Long.toString(diffWeeks)+" D: "+Long.toString(diffDays)+" H: "+String.format("%02d", diffHours)+" M: "+String.format("%02d", diffMinutes)+" S: "+String.format("%02d", diffSeconds);
return difference;
}
Run Code Online (Sandbox Code Playgroud)