我有一些问题。我是一个初学者程序员。我需要使用Android Studio创建一个类似于时钟的应用程序,并且需要增加和减少小时数和分钟数。当我按+ 1H时,需要加上1H才能与+ 1M和-1M相同,但我无法协调该怎么做。 我这样做是为了当时钟再次到达23:59时是00:00,如此周期性地
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button button,button2,button3,button4;
private TextView textView;
int hour = 0;
int minute = 0;
int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button2 = findViewById(R.id.button2);
button3 = findViewById(R.id.button3);
button4 = findViewById(R.id.button4);
textView = findViewById(R.id.res);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (hour >= 9) {
textView.setText((++hour + "") + ":" + "00");
} else {
textView.setText("0" + (++hour + "") + ":" + "00");
}
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(hour>=9) {
textView.setText((--hour + "") + ":" + "00");
}else{
textView.setText("0"+(--hour + "") + ":" + "00");
}
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(minute>=9) {
textView.setText("00" + ":" + (++minute + ""));
}else{
textView.setText("00" + ":" + "0"+(++minute + ""));
}
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(minute>=9) {
textView.setText("00" + ":" + (--minute + ""));
}else{
textView.setText("00" + ":" + "0"+(--minute + ""));
}
}
});
}
}
答案 0 :(得分:1)
所有这些功能已经存在。使用Java.time(现代Java日期和时间API)中的LocalTime
。这是一个简短的演示,并在注释中给出输出:
LocalTime time = LocalTime.of(23, 59);
System.out.println(time); // 23:59
time = time.minusHours(1);
System.out.println(time); // 22:59
time = time.minusMinutes(1);
System.out.println(time); // 22:58
time = time.plusHours(1);
System.out.println(time); // 23:58
time = time.plusMinutes(1);
System.out.println(time); // 23:59
time = time.plusHours(1);
System.out.println(time); // 00:59
time = time.plusMinutes(1);
System.out.println(time); // 01:00
是的,java.time在较新和较旧的Android设备上均可正常运行。它只需要至少 Java 6 。
org.threeten.bp
导入日期和时间类。java.time
。java.time
向Java 6和7(JSR-310的ThreeTen)的反向端口。答案 1 :(得分:0)
也许这段代码将帮助您获取时间并正确地增加或减少时间。
Calendar calendar = Calendar.getInstance();
calendar.setTime(yourdate);
int hours = calendar.get(Calendar.HOUR_OF_DAY);
int minutes = calendar.get(Calendar.MINUTE);
int seconds = calendar.get(Calendar.SECOND);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
if(hours>=9)
{
hours=hours+1;
textView.setText(hours+":"+minutes+":"+seconds);
}
else
{
textView.setText(hours+":"+minutes+":"+seconds);
}
}
});
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(hours>=9)
{
hours=hours-1;
textView.setText(hours+":"+minutes+":"+seconds);
}
else
{
textView.setText(hours+":"+minutes+":"+seconds);
}
}
});
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(minutes>=9)
{
minutes=minutes+1;
textView.setText(hours+":"+minutes+":"+seconds);
}
else
{
textView.setText(hours+":"+minutes+":"+seconds);
}
}
});
b3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(minutes>=9)
{
minutes=mminutes-1;
textView.setText(hours+":"+minutes+":"+seconds);
}
else
{
textView.setText(hours+":"+minutes+":"+seconds);
}
}
});
答案 2 :(得分:0)
为此使用Calendar#add
。令人困惑的是,您可以使用它同时进行加减。
final Calendar calendar = Calendar.getInstance(); //A calendar set to the current time
calendar.add(Calendar.HOUR_OF_DAY, 1); //Add one hour
calendar.add(Calendar.HOUR_OF_DAY, -1); //Subtract one hour
关于打印时间,API版本<24的默认选项是使用DateFormat
。因此,与其尝试自己决定,还不如:
final SimpleDateFormat format = new SimpleDateFormat("HH:mm"); //hours and minutes, 24hr clock
textView.setText(format.format(calendar.getTime());
但是,请注意SimpleDateFormat
存在问题,尤其是在多线程方面。作为Ole V.V.已在评论中建议,您应该考虑改用ThreeTenABP
库。
如果您的目标是API 24+,则可以随时使用java.time
包中的Java 8附加功能,例如Instant
和DateTimeFormatter