Reputation: 441
I want to make a countdown timer of New year from current time in android...
Calendar thatDay = Calendar.getInstance();
thatDay.set(Calendar.DAY_OF_MONTH,1);
thatDay.set(Calendar.MONTH,0); // 0-11 so 1 less
thatDay.set(Calendar.YEAR, 2014);
Calendar today = Calendar.getInstance();
long diff = thatDay.getTimeInMillis() - today.getTimeInMillis();
Now how to convert this diff into day,hour,minute,seconds format? please help
Upvotes: 2
Views: 9551
Reputation: 895
the following code
import java.util.Date;
import java.util.Calendar;
public class cal {
public static int SECONDS_IN_A_DAY = 24 * 60 * 60;
public static void main(String[] args) {
Calendar thatDay = Calendar.getInstance();
thatDay.setTime(new Date(0)); /* reset */
thatDay.set(Calendar.DAY_OF_MONTH,1);
thatDay.set(Calendar.MONTH,0); // 0-11 so 1 less
thatDay.set(Calendar.YEAR, 2014);
Calendar today = Calendar.getInstance();
long diff = thatDay.getTimeInMillis() - today.getTimeInMillis();
long diffSec = diff / 1000;
long days = diffSec / SECONDS_IN_A_DAY;
long secondsDay = diffSec % SECONDS_IN_A_DAY;
long seconds = secondsDay % 60;
long minutes = (secondsDay / 60) % 60;
long hours = (secondsDay / 3600); // % 24 not needed
System.out.printf("%d days, %d hours, %d minutes and %d seconds\n", days, hours, minutes, seconds);
}
}
produces
27 days, 17 hours, 18 minutes and 2 seconds
27 days, 17 hours, 18 minutes and 1 seconds
27 days, 17 hours, 18 minutes and 0 seconds
27 days, 17 hours, 17 minutes and 59 seconds
27 days, 17 hours, 17 minutes and 58 seconds
27 days, 17 hours, 17 minutes and 57 seconds
27 days, 17 hours, 17 minutes and 56 seconds
27 days, 17 hours, 17 minutes and 55 seconds
27 days, 17 hours, 17 minutes and 54 seconds
27 days, 17 hours, 17 minutes and 53 seconds
hope this is what you want
Upvotes: 5
Reputation: 8645
Try this one
String formatStr = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf1 = new SimpleDateFormat(formatStr);
TimeZone obj = TimeZone.getTimeZone("CST");
sdf1.setTimeZone(obj);
Date date = sdf1.parse("your time"); // here put your time zone
long millis = date.getTime();
long currentTimeInMili = new Date().getTime();
MyCount counter = new MyCount(millis - currentTimeInMili, 1 * 1000);
counter.start();
class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}// MyCount
public void onPause() {
onPause();
}// finish
public void onTick(long millisUntilFinished) {
dynamicMatchDesTextView.setText(""+ formatTime(millisUntilFinished)+ " till match start");
}// on tick
@Override
public void onFinish() {
onStop();
}// finish
}
public String formatTime(long millis) {
String output = "00:00";
try {
long seconds = millis / 1000;
long minutes = seconds / 60;
long hours = seconds / 3600;
long days = seconds / (3600 * 24);
seconds = seconds % 60;
minutes = minutes % 60;
hours = hours % 24;
days = days % 30;
String sec = String.valueOf(seconds);
String min = String.valueOf(minutes);
String hur = String.valueOf(hours);
String day = String.valueOf(days);
if (seconds < 10)
sec = "0" + seconds;
if (minutes < 10)
min = "0" + minutes;
if (hours < 10)
hur = "0" + hours;
if (days < 10)
day = "0" + days;
output = day + "D " + hur + "H " + min + "M " + sec + "S";
} catch (Exception e) {
e.printStackTrace();
}
return output;
}
Upvotes: 1
Reputation: 454
SimpleDateFormat: This will accomplish your goal quite easily.
Simply do the following..
SimpleDateFormat fmt = new SimpleDateFormat... (read the java docs)
fmt.format(your string)
No need to re-invent the wheel and make your program larger... This code already exists in Java.
EDIT: Since OP can't read.. S Millisecond Number 978
Upvotes: -2
Reputation: 44813
int SECONDS = 1000;
int MINUTE = SECONDS * 60 ;
int HOUR = MINUTE * 60;
int DAY = HOUR * 24;
I would start with diff and divide by DAY and then modula by DAY to get the remainder for the next part.
repeat for HOUR, and MINUTE and SECOND
Upvotes: 0
Reputation: 17622
Calendar thatDay = Calendar.getInstance();
thatDay.set(Calendar.DAY_OF_MONTH,1);
thatDay.set(Calendar.MONTH,0); // 0-11 so 1 less
thatDay.set(Calendar.YEAR, 2014);
thatDay.set(Calendar.HOUR_OF_DAY, 0);
thatDay.set(Calendar.MINUTE, 0);
System.out.println(thatDay.getTime());
Calendar today = Calendar.getInstance();
System.out.println(today.getTime());
int diffInYears = thatDay.get(Calendar.YEAR) - today.get(Calendar.YEAR);
int diffInDays = ((diffInYears*365) + thatDay.get(Calendar.DAY_OF_YEAR)) - today.get(Calendar.DAY_OF_YEAR);
diffInDays--; // Decrementing because I will be taking 1 day (which is 24 hours) to do below hours calculation
int diffInHours = thatDay.get(Calendar.HOUR_OF_DAY)+23 - today.get(Calendar.HOUR_OF_DAY); // Used 23 hours instead of 24 hours because I am using 1 hour (60 minutes) to do below calculation
int diffInMins = thatDay.get(Calendar.MINUTE)+60 - today.get(Calendar.MINUTE);
System.out.println(diffInDays + " days " + diffInHours + " hours " + diffInMins +" minutes remaining");
Upvotes: 0
Reputation: 137
You can use this:
public String formatDate( long milliSec ) {
long diffSec = milliSec / (1000);
long diffMinutes = milliSec / (60 * 1000) % 60;
long diffHours = milliSec / (60 * 60 * 1000) % 24;
long diffDays = milliSec / (24 * 60 * 60 * 1000);
StringBuilder date = new StringBuilder();
if(diffDays != 0){
date.append(diffDays).append("D ");
}
if(diffHours != 0){
int length = (int)(Math.log10(diffHours)+1);
if(length == 1){
String s = "0"+diffHours;
date.append(s).append(":");
}else{
date.append(diffHours).append(":");
}
}else{
date.append("00").append(":");
}
if(diffMinutes != 0 ){
int length = (int)(Math.log10(diffMinutes)+1);
if(length == 1){
String s = "0"+diffMinutes;
date.append(s).append(":");
}else{
date.append(diffMinutes).append(":");;
}
}else{
date.append("00");
}
if(diffSec != 0){
int length = (int)(Math.log10(diffSec)+1);
if(length == 1){
String s = "0"+diffSec;
date.append(s);
}else{
date.append(diffSec);
}
}else{
date.append("00");
}
return date.toString();
}
Upvotes: 0