在我的程序中,我必须显示CountDown Timer,为此我编写了一些代码,这些代码允许我获得天,小时,分钟和秒,但不知道如何计算年,月和周。< / p>
我仍然得到这个:
days:384 hours:6 minutes:27 seconds:25
但我需要这个:
year:1 months:2 weeks:5 days:125 hours:6 minutes:27 seconds:25
检查我的以下代码:
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);
}
}
答案 0 :(得分:1)
我有这个代码对我有用,而且更简单:
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;
}
它返回一个String,但您可以轻松地将其调整为我认为的代码。
对于月份和年份,我使用日历(将其添加到我的代码中):
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);
当年度发生变化时,您需要添加一些魔力,但这就是想法。
编辑:请使用这个更完整的代码进行一些测试:
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;
}
答案 1 :(得分:1)
或者您可以简单地使用Joda-Time库来执行此操作
Interval interval = new Interval(startDate.getTime(), endDate.getTime());
Period period = interval.toPeriod();
String strDiff = "Year: " + period.getYears() + "/ Month: " + period.getMonths() + "/ Days: " + period.getDays() + "/ Hours: " + period.getHours() + "/ Minutes: " + period.getMinutes() + "/ Seconds: " + period.getSeconds();
答案 2 :(得分:1)
我希望这会有所帮助:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_count_down);
final TextView time = (TextView)findViewById(R.id.txtCountDown);
final GregorianCalendar currentTime=new GregorianCalendar (2014,9,1,9,16,0);
GregorianCalendar targetTime=new GregorianCalendar (2016,5,1,11,2,0);
long difference = targetTime.getTimeInMillis() - currentTime.getTimeInMillis();
new CountDownTimer(difference,1000) {
@Override
public void onTick(long arg0) {
Calendar countdownDuration = Calendar.getInstance();
countdownDuration.setTimeInMillis(currentTime.getTimeInMillis() + arg0);
int year = countdownDuration.get(Calendar.YEAR)-currentTime.get(Calendar.YEAR);
int month = countdownDuration.get(Calendar.MONTH);
int day = countdownDuration.get(Calendar.DAY_OF_MONTH)-1;
int hour = countdownDuration.get(Calendar.HOUR);
int minute = countdownDuration.get(Calendar.MINUTE);
int sec = countdownDuration.get(Calendar.SECOND);
time.setText("Years: " + year + "\nMonth:" + month + "\nDays:" + day +"\nHour:" + hour
+ "\nMinute" + minute + "\nSecond:" + sec);
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
}
}.start();
}
答案 3 :(得分:0)
private static long converTimeStringINToMillis(String time) {
long milliseconds = 0;
try {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
// 25/06/2014 8:41:26
Date date;
date = sdf.parse(time);
milliseconds = date.getTime();
} catch (ParseException e) {
// TODO Auto-generated catch block
milliseconds = 0;
e.printStackTrace();
}
return milliseconds;
}
public static String setLastSeenTime(String time){
long milliseconds = Math.abs(System.currentTimeMillis() - converTimeStringINToMillis(time));
String lastSeen = "";
int seconds = (int)milliseconds/1000;
if(seconds < 60)
lastSeen = String.valueOf(seconds) + "sec ago";
else if(seconds >60 && seconds < 3600)
lastSeen = String.valueOf((int)seconds/60) + " min ago";
else if(seconds > 3600 && seconds < 86400)
lastSeen = String.valueOf((int)seconds/3600) + " hours ago";
else if(seconds > 86400 && seconds < 172800)
lastSeen =" Yesterday";
else if(seconds > 172800 && seconds < 2592000)
lastSeen = String.valueOf((int)(seconds/(24*3600))) + " days ago";
else if(seconds > 2592000)
lastSeen = String.valueOf((int)(seconds/(30*24*3600))) + " months ago";
return lastSeen;
}
添加你的代码..