2つの時間の差をhh:mm形式で表示したい。
1回目はデータベースからのもので、2回目はシステム時間です。時差は毎秒更新されます。
どうやってやるの?
現在、2つの手動時間を使用しています。これが完全に機能する場合は、アプリに実装します。
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);
}
}
2つの日付の差を計算するには、次のようなものを試すことができます。
long mills = date1.getTime() - date2.getTime();
int hours = millis/(1000 * 60 * 60);
int mins = (mills/(1000*60)) % 60;
String diff = hours + ":" + mins;
1秒ごとに時差を更新するには、タイマーを使用できます。
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
編集:作業コード:
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);
}
ついにやった.
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;
}
}
プロセスはおおよそ次のとおりです。
次の方法で文字列インスタンスを日付インスタンスに変換します
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse("2011-01-03");
あなたが持っているsystemTimeがepocからミリ秒を表す長いと仮定すると、次のことができるようになりました
long difference = longNow - date.getTime();
int msPerHour = 1000*60*60;
int hours = difference/secondPerHour;
int minutes = difference % secondPerHour;
ここで、longNow
はシステム時間を含む現在の変数です。
OK私はあなたのためにここで機能を構築します:
public void omriFunction(){
Date Start = null;
Date End = null;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
try {
Start = simpleDateFormat.parse(04+":"+30);
End = simpleDateFormat.parse(06+":"+45);}
catch(ParseException e){
//Some thing if its not working
}
long difference = End.getTime() - Start.getTime();
int days = (int) (difference / (1000*60*60*24));
int hours = (int) ((difference - (1000*60*60*24*days)) / (1000*60*60));
int min = (int) (difference - (1000*60*60*24*days) - (1000*60*60*hours)) / (1000*60);
if(hours < 0){
hours+=24;
}if(min < 0){
float newone = (float)min/60 ;
min +=60;
hours =(int) (hours +newone);}
String c = hours+":"+min;
Log.d("ANSWER",c);}
答え:2:15; logcatで
Date currentTime = parseDate("11:27:20 AM");
Date endTime = parseDate("10:30:01 AM");
if (endTime.before(currentTime))
{
Log.e("Time :","===> is before from current time");
}
if (endTime.after(currentTime))
{
Log.e("Time :","===> is after from current time");
}
private Date parseDate(String date)
{
String inputFormat = "hh:mm:ss aa";
SimpleDateFormat inputParser = new SimpleDateFormat(inputFormat, Locale.US);
try {
return inputParser.parse(date);
} catch (Java.text.ParseException e) {
return new Date(0);
}
}
こんにちはGuysは、私が何を間違っていたのかわかりませんが、これは私を助けてくれました。
私の最小値はフロート形式で計算されていたため、この式を使用しました
long Min = time % (1000*60*60)/(60*1000);
time is my date2.getTime() - date1.getTime();
ハッピーコーディング
トレイ2つの時間で異なる時間と分を取得する次のコード:
private static int getHoursDiff(Calendar c1, Calendar c2) {
Date d1 = c1.getTime();
Date d2 = c2.getTime();
long mils = d1.getTime() - d2.getTime();
int hourDiff = (int) (mils / (1000 * 60 * 60));
return hourDiff;
}
private static int getMinuteDiff(Calendar c1, Calendar c2) {
Date d1 = c1.getTime();
Date d2 = c2.getTime();
long mils = d1.getTime() - d2.getTime();
int minuteFor = (int) (mils / (1000 * 60) % 60);
return minuteFor;
} }