gpt4 book ai didi

java - 减去两个时间字符串

转载 作者:行者123 更新时间:2023-12-01 10:16:15 24 4
gpt4 key购买 nike

我有两个字符串变量,分别称为“clockedin”和“clockedout”。我将它们放在共享首选项中,现在我尝试调用它们并将它们转换为整数,这样我就可以从上类时间中减去下类时间来查看工作了多少小时。

    public void onCheckedChanged(CompoundButton compoundButton, boolean b){
if(b){
Calendar c = Calendar.getInstance();
System.out.println(c.getTime());
SimpleDateFormat df = new SimpleDateFormat("hh:mm a");
String formattedDate = df.format(c.getTime());
TextView clockedin = (TextView)findViewById(R.id.clockedin);
clockedin.setText(formattedDate);






SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("toggleButton", tb.isChecked());
editor.commit();

SharedPreferences sharedPreferences2=getSharedPreferences("MyData", Context.MODE_PRIVATE);
SharedPreferences.Editor editor1= sharedPreferences2.edit();
editor1.putString("clockedin", formattedDate);
editor1.commit();
}
else {
Calendar c = Calendar.getInstance();
System.out.println(c.getTime());
SimpleDateFormat df = new SimpleDateFormat("hh:mm a");
String formattedDate = df.format(c.getTime());
TextView clockedout = (TextView)findViewById(R.id.clockedout);
clockedout.setText(formattedDate);

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("toggleButton", false);
editor.commit();

SharedPreferences sharedPreferences2=getSharedPreferences("MyData2", Context.MODE_PRIVATE);
SharedPreferences.Editor editor1= sharedPreferences2.edit();
editor1.putString("clockedout", clockedout.getText().toString());
editor1.commit();

SharedPreferences sharedPreferences1 = getSharedPreferences("MyData", Context.MODE_PRIVATE);
String clockedin =sharedPreferences1.getString("clockedin", DEFAULT);


}}

我的转换方法:

    public void onClickButtonListener2(){
Button button2;
button2 = (Button) findViewById(R.id.calctotal);
button2.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Date date = null;
Date date2 = null;


SharedPreferences sharedPreferences1 = getSharedPreferences("MyData", Context.MODE_PRIVATE);
String clockedin =sharedPreferences1.getString("clockedin", DEFAULT);

SharedPreferences sharedPreferences2 = getSharedPreferences("MyData2", Context.MODE_PRIVATE);
String clockedout =sharedPreferences2.getString("clockedout",DEFAULT);

DateFormat outFormat = new SimpleDateFormat("hh:mm");
try {
date = outFormat.parse(String.valueOf(clockedout));
date2 = outFormat.parse(clockedin);
}catch ( ParseException e){
e.printStackTrace();
}
if (date2.before(date)){
long diffMs = date.getTime() - date2.getTime();
long diffSec = diffMs/1000;
long min = diffSec/60;
double hour = (double)min/60;
double hourA = Math.round(hour *100.0)/100.0;
TextView totalhours=(TextView)findViewById(R.id.totalhours);
totalhours.setText((int) hourA);

}

}
});
}

附上我的日志enter image description here

最佳答案

totalhours.setText((int) hourA);

应该是:totalhours.setText(String.valueOf(hourA));或者Totalhours.setText(""+ hourA);

如果您使用整数调用 setText,它将查找具有该 ID 的字符串资源。

关于java - 减去两个时间字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35874759/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com