بروز خطا
[message]
اشتراک در سوال
رای ها
[dataList]
درخواست راهنمایی برای رفع باگ روزشمار
سلام دوستان
خسته نباشید
من دارم یه روزشمار میسازم. تقریبا همه چیش درست کار میکنه اما دوتا ایراد داره
1. توی محاسبه تعداد ماه ها درست عمل نمیکنه
2. پشت تعداد ماه ها منفی میاد با وجود اینکه من زمان آینده رو از گذشته کم میکنم
کد:
long difference = futureDate.getTime() - currentDate.getTime();
long months = difference / (30 * 24 * 60 * 60 * 1000);
difference -= months * (30 * 24 * 60 * 60 * 1000);
long days = difference / (24 * 60 * 60 * 1000);
difference -= days * (24 * 60 * 60 * 1000);
long hours = difference / (60 * 60 * 1000);
difference -= hours * (60 * 60 * 1000);
long minutes = difference / (60 * 1000);
difference -= minutes * (60 * 1000);
long seconds = difference / 1000;
txt_month.setText("" + String.format("%02d", months));
txt_day.setText("" + String.format("%02d", days));
txt_hour.setText("" + String.format("%02d", hours));
txt_min.setText("" + String.format("%02d", minutes));
txt_scd.setText("" + String.format("%02d", seconds));
ممنون میشم راهنمایی کنید
برای این سوال 3 پاسخ وجود دارد.
مشاهده پاسخ صحیح
پاسخ به سوال
میلاد محمدی
6 سال پیش
0
0
اون عبارتی که گفتید فرمت تاریخ هست که باید تعیین کنم:
("SimpleDateFormat") SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date futureDate = dateFormat.parse(examTime);
Date currentDate = new Date();
پاسخ به سوال
میلاد محمدی
6 سال پیش
0
0
public void countDownStart(final String examTime) {
handler = new Handler();
runnable = new Runnable() {
@SuppressLint("DefaultLocale")
@Override
public void run() {
handler.postDelayed(this, 1000);
try {
@SuppressLint("SimpleDateFormat") SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
Date futureDate = dateFormat.parse(examTime);
Date currentDate = new Date();
if (!currentDate.after(futureDate)) {
long difference = futureDate.getTime() - currentDate.getTime();
long months = difference / (30 * 24 * 60 * 60 * 1000);
difference -= months * (30 * 24 * 60 * 60 * 1000);
long days = difference / (24 * 60 * 60 * 1000);
difference -= days * (24 * 60 * 60 * 1000);
long hours = difference / (60 * 60 * 1000);
difference -= hours * (60 * 60 * 1000);
long minutes = difference / (60 * 1000);
difference -= minutes * (60 * 1000);
long seconds = difference / 1000;
txt_month.setText("" + String.format("%02d", months));
txt_day.setText("" + String.format("%02d", days));
txt_hour.setText("" + String.format("%02d", hours));
txt_min.setText("" + String.format("%02d", minutes));
txt_scd.setText("" + String.format("%02d", seconds));
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
handler.postDelayed(runnable, 1000);
}
پاسخ به سوال
میلاد محمدی
6 سال پیش
0
0
پاسخ صحیح
راه حلش رو پیدا کردم.کد رو قرار میدم اگر کسی نیاز داشت استفاده کنه
public void countDownStart(final String examTime) {
handler = new Handler();
runnable = new Runnable() {
@SuppressLint("DefaultLocale")
@Override
public void run() {
handler.postDelayed(this, 1000);
try {
@SuppressLint("SimpleDateFormat") SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date futureDate = dateFormat.parse(examTime);
Date currentDate = new Date();
if (!currentDate.after(futureDate)) {
long difference = futureDate.getTime() - currentDate.getTime();
long totalDays = difference / (24 * 60 * 60 * 1000);
long day = TimeUnit.MILLISECONDS.toDays(difference);
day %= 365;
long month = day / 30;
day %= 30;
long hour = TimeUnit.MILLISECONDS.toHours(difference) - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(difference));
long min = TimeUnit.MILLISECONDS.toMinutes(difference) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(difference));
long sec = TimeUnit.MILLISECONDS.toSeconds(difference) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(difference));
txt_month.setText("" + String.format("%02d", month));
txt_day.setText("" + String.format("%02d", day));
txt_hour.setText("" + String.format("%02d", hour));
txt_min.setText("" + String.format("%02d", min));
txt_scd.setText("" + String.format("%02d", sec));
txt_mandeh.setText("" + String.format("%02d", totalDays) + " " + "روز تا زمان تعیین شده باقی مانده است!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
handler.postDelayed(runnable, 1000);
}
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .
Code("SimpleDateFormat") SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HHss"); Date futureDate = dateFormat.parse(examTime); Date currentDate = new Date();Code(6 سال پیش)