简单日期倒计时 - Android

时间:2013-12-29 20:32:56

标签: android date countdown

我正在尝试为第12天和第06天创建一个倒计时,但我不知道该怎么办。我所拥有的是:

Calendar c = Calendar.getInstance(); 
int day = c.get(Calendar.DAY_OF_MONTH);
int month = c.get(Calendar.MONTH);

// set because java calendar starts in 0       
month = month + 1;

//Lefting months           
int leftMonth = 06 - mes;

//Lefting days
int leftDay = 12 - dia;

text.setText("Faltam " + leftMonth + " Meses e " + leftDay + " Dias");

任何人都可以帮助我吗?谢谢!

2 个答案:

答案 0 :(得分:0)

    new CountDownTimer(30000, 1000)   {//CountDownTimer(edittext1.getText()+edittext2.getText()) also parse it to long
 public void onTick(long millisUntilFinished) {
     mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
  //here you can have your logic to set text to edittext
 }
 public void onFinish() {
     mTextField.setText("done!");
 }
}
.start();

关于countDown计时器更多细节,您可以阅读there

首先,你必须知道你的约会的里程数。

Date date=new Date();
System.out.println(date.getTime()); //this will return miliseconds from 1970 year to now
String str = "Jun 13 2023 23:11:52.454 UTC";
SimpleDateFormat df = new SimpleDateFormat("MMM dd yyyy HH:mm:ss.SSS zzz");
Date date = df.parse(str);
long epoch = date.getTime();//this will return miliseconds from 1970 to  Jun 13 2023

答案 1 :(得分:0)

Joda-Time库使日期时间计算更容易更多

Joda-Time提供DurationPeriod类来定义两个时刻之间的时间跨度。 PeriodFormatter描述的是以年,月,日,小时,分钟和秒为单位的范围。您可以使用PeriodFormatterBuilder定义自己的格式,但内置的格式可能就足够了。 Joda-Time甚至可以通过使用Java Locale类来帮助进行本地化。

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
// import org.joda.time.format.*;

DateTimeZone timeZone = DateTimeZone.forID( "America/Montreal" );

// Calculate time until the new year.
DateTime target = new DateTime( 2014, 1, 1, 0, 0, 0, timeZone );
DateTime now = new DateTime( timeZone );

Period period = new Period( now, target );

在各种语言的文本中呈现表示......

// Using default language of this Java Virtual Machine.
PeriodFormatter formatter = PeriodFormat.getDefault();
String output = formatter.print( period );

// Using French language.
PeriodFormatter formatterEnFrançais = PeriodFormat.wordBased( Locale.CANADA_FRENCH );
String outputEnFrançais = formatterEnFrançais.print( period );

// Using Portugese language.
PeriodFormatter formatterPortugese = PeriodFormat.wordBased( Locale.forLanguageTag( "pt" ) );
String outputPortugese = formatterPortugese.print( period );

转储到控制台...

System.out.println( "output: " + output );
System.out.println( "outputEnFrançais: " + outputEnFrançais );
System.out.println( "outputPortugese: " + outputPortugese );

跑步时......

output: 2 days, 7 hours, 44 minutes, 1 second and 955 milliseconds
outputEnFrançais: 2 jours, 7 heures, 44 minutes, 1 seconde et 955 millisecondes
outputPortugese: 2 dias, 7 horas, 44 minutos, 1 segundo e 955 milissegundos

买者

有些人报告说,在Android中第一次拨打Joda-Time时会出现放缓。永远不会发生在proper Java环境中,只有Android。我不知道这个问题的现状。