【发布时间】:2013-03-12 11:38:10
【问题描述】:
我想以 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);
}
}
【问题讨论】:
-
数据库中的那个是字符串还是日期实例?
-
差异有多大(秒、毫秒、天...)?
-
你已经尝试过了吗?
-
@assylias 我需要 hh:mm 格式,例如上午 11:05 - 下午 12:30 = 01:25
-
@JustDanyul 字符串实例