我希望以hh:mm格式显示两次之间的差异.
第一次是来自数据库,第二次是系统时间.时差每秒更新一次.
我怎样才能做到这一点?
目前我正在使用两个手动时间如果这完全有效,那么我将它实现到我的应用程序中.
public class MainActivity extends Activity
{
TextView mytext;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Timer updateTimer = new Timer();
updateTimer.schedule(new TimerTask()
{
public void run()
{
try
{
TextView txtCurrentTime= (TextView)findViewById(R.id.mytext);
SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss aa");
Date date1 = format.parse("08:00:12 pm");
Date date2 = format.parse("05:30:12 pm");
long mills = date1.getTime() - date2.getTime();
Log.v("Data1", ""+date1.getTime());
Log.v("Data2", ""+date2.getTime());
int hours = (int) (mills/(1000 * 60 * 60));
int mins = (int) (mills % (1000*60*60));
String diff = hours + ":" + mins; // updated value every1 second
txtCurrentTime.setText(diff);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}, 0, 1000);
}
}
Run Code Online (Sandbox Code Playgroud)
San*_*lik 34
要计算两个日期之间的差异,您可以尝试以下方法:
long mills = date1.getTime() - date2.getTime();
int hours = millis/(1000 * 60 * 60);
int mins = (mills/(1000*60)) % 60;
String diff = hours + ":" + mins;
Run Code Online (Sandbox Code Playgroud)
要每秒更新时差,您可以使用Timer.
Timer updateTimer = new Timer();
updateTimer.schedule(new TimerTask() {
public void run() {
try {
long mills = date1.getTime() - date2.getTime();
int hours = millis/(1000 * 60 * 60);
int mins = (mills/(1000*60)) % 60;
String diff = hours + ":" + mins; // updated value every1 second
} catch (Exception e) {
e.printStackTrace();
}
}
}, 0, 1000); // here 1000 means 1000 mills i.e. 1 second
Run Code Online (Sandbox Code Playgroud)
编辑:工作代码:
public class MainActivity extends Activity {
private TextView txtCurrentTime;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtCurrentTime= (TextView)findViewById(R.id.mytext);
Timer updateTimer = new Timer();
updateTimer.schedule(new TimerTask()
{
public void run()
{
try
{
SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss aa");
Date date1 = format.parse("08:00:12 pm");
Date date2 = format.parse("05:30:12 pm");
long mills = date1.getTime() - date2.getTime();
Log.v("Data1", ""+date1.getTime());
Log.v("Data2", ""+date2.getTime());
int hours = (int) (mills/(1000 * 60 * 60));
int mins = (int) (mills/(1000*60)) % 60;
String diff = hours + ":" + mins; // updated value every1 second
txtCurrentTime.setText(diff);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}, 0, 1000);
}
Run Code Online (Sandbox Code Playgroud)
终于做到了yuppiiieee ...
package com.timedynamicllyupdate;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity
{
TextView current;
private TextView txtCurrentTime;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread myThread = null;
Runnable myRunnableThread = new CountDownRunner();
myThread= new Thread(myRunnableThread);
myThread.start();
current= (TextView)findViewById(R.id.current);
}
public void doWork()
{
runOnUiThread(new Runnable()
{
public void run()
{
try
{
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss aa");
txtCurrentTime= (TextView)findViewById(R.id.mytext);
Date systemDate = Calendar.getInstance().getTime();
String myDate = sdf.format(systemDate);
// txtCurrentTime.setText(myDate);
Date Date1 = sdf.parse(myDate);
Date Date2 = sdf.parse("02:50:00 pm");
long millse = Date1.getTime() - Date2.getTime();
long mills = Math.abs(millse);
int Hours = (int) (mills/(1000 * 60 * 60));
int Mins = (int) (mills/(1000*60)) % 60;
long Secs = (int) (mills / 1000) % 60;
String diff = Hours + ":" + Mins + ":" + Secs; // updated value every1 second
current.setText(diff);
}
catch (Exception e)
{
}
}
});
}
class CountDownRunner implements Runnable
{
// @Override
public void run()
{
while(!Thread.currentThread().isInterrupted())
{
try
{
doWork();
Thread.sleep(1000); // Pause of 1 Second
}
catch (InterruptedException e)
{
Thread.currentThread().interrupt();
}
catch(Exception e)
{
}
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
现代的方法是使用java.time类来取代麻烦的旧日期时间类.
该LocalTime级表示没有日期,没有一个时区时间的一天.
使用DateTimeFormatter类定义格式模式.
String inputStart = "08:00:12 pm".toUpperCase() ;
String inputStop = "05:30:12 pm".toUpperCase() ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "hh:mm:ss a" );
LocalTime start = LocalTime.parse( inputStart , f );
LocalTime stop = LocalTime.parse( inputStop , f );
Run Code Online (Sandbox Code Playgroud)
start.toString():20:00:12
stop.toString():17:30:12
该LocalTime课程在一个通用的24小时工作日内完成.所以它不考虑过夜.如果您想在几天之间交叉,那么您应该使用ZonedDateTime,OffsetDateTime或者LocalDateTime代之以所有日期时间对象,而不是仅限时间.
A Duration捕获未附加到时间线的时间跨度.
Duration d = Duration.between( start , stop );
Run Code Online (Sandbox Code Playgroud)
呼叫toString以标准ISO 8601 格式生成持续时间的文本:PnYnMnDTnHnMnS其中P标记开头并将T年 - 月 - 天与小时 - 分 - 秒分开.我强烈建议使用这种格式而不是时钟时间不明确的"HH:MM:SS"格式.
如果您坚持使用模棱两可的时钟时间格式,在Java 9,以后你可以通过调用建立一个字符串toHoursPart,toMinutesPart和toSecondsPart.
在您的示例数据中,我们正在向后移动,从晚上8点到下午5点,结果是负数小时和分钟,负两个半小时.
d.toString():PT-2H-30M
请参阅IdeOne.com上的此代码.
该java.time框架是建立在Java 8和更高版本.这些类取代麻烦的老传统日期时间类,如java.util.Date,Calendar,和SimpleDateFormat.
现在处于维护模式的Joda-Time项目建议迁移到java.time类.
要了解更多信息,请参阅Oracle教程.并搜索Stack Overflow以获取许多示例和解释.规范是JSR 310.
从哪里获取java.time类?
该ThreeTen-额外项目与其他类扩展java.time.该项目是未来可能添加到java.time的试验场.您可以在此比如找到一些有用的类Interval,YearWeek,YearQuarter,和更多.
| 归档时间: |
|
| 查看次数: |
56836 次 |
| 最近记录: |