【发布时间】:2016-06-02 04:05:28
【问题描述】:
如何将 12 小时 hh:mm a 转换为 HH:mm?当我们转换它时,我们无法进入 24 小时格式。当我们指定晚上 10 点开始时间和早上 7 点起床时间时,这里的时间格式 pm 到 am 没有转换。在这里,我们无法获得总时间。它在上午 12:00 停止,而且我们从晚上 10 点到早上 7 点的总时间翻了一番,为 18 小时。当时间从晚上 11:59 变为上午 00:00 "00:00am" 时,这里会出现问题。
public class Wakeup extends Activity {
ImageButton home, back, up_arw2, up_arw1, up_arw3, down_arw4, down_arw5, down_arw6;
TextView hours, minutes, ampm;
Button save_btn;
SharedPreferences timepreference;
SharedPreferences.Editor edittime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wakeup_time);
hours = (TextView) findViewById(R.id.hours);
minutes = (TextView) findViewById(R.id.minutes);
ampm = (TextView) findViewById(R.id.ampm);
home = (ImageButton) findViewById(R.id.home);
save_btn = (Button) findViewById(R.id.save_btn);
timepreference = getSharedPreferences("CHILDTIME", MODE_PRIVATE);
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("HH:mm a");
String formattedTime = df.format(c.getTime());
edittime = timepreference.edit();
home.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), Settings.class));
}
});
back = (ImageButton) findViewById(R.id.back);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), Settings.class));
}
});
up_arw2 = (ImageButton) findViewById(R.id.up_arw2);
up_arw2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int timenum = Integer.parseInt(hours.getText().toString());
if (timenum == 11) {
if (ampm.getText().toString().equals("AM")) {
ampm.setText("PM");
timenum++;
hours.setText(String.valueOf(timenum));
} else {
ampm.setText("AM");
timenum++;
hours.setText(String.valueOf(timenum));
}
} else {
timenum++;
hours.setText(String.valueOf(timenum));
down_arw4.setClickable(true);
}
if (timenum > 12) {
hours.setText("1");
}
}
});
down_arw4 = (ImageButton) findViewById(R.id.down_arw4);
down_arw4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int timenum = Integer.parseInt(hours.getText().toString());
if (timenum == 1) {
hours.setText("12");
} else {
timenum--;
hours.setText(String.valueOf(timenum));
}
}
});
up_arw3 = (ImageButton) findViewById(R.id.up_arw3);
up_arw3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int timenum = Integer.parseInt(minutes.getText().toString());
if (timenum == 59) {
minutes.setText("00");
} else {
timenum++;
minutes.setText(roundedmin(String.valueOf(timenum)));
}
}
});
down_arw5 = (ImageButton) findViewById(R.id.down_arw5);
down_arw5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int timenum = Integer.parseInt(minutes.getText().toString());
if (timenum == 0) {
minutes.setText("59");
} else {
timenum--;
minutes.setText(roundedmin(String.valueOf(timenum)));
up_arw3.setClickable(true);
}
}
});
down_arw6 = (ImageButton) findViewById(R.id.down_arw6);
down_arw6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ampm.setText("PM");
}
});
up_arw1 = (ImageButton) findViewById(R.id.up_arw1);
up_arw1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ampm.setText("AM");
}
});
save_btn.setOnClickListener(new View.OnClickListener() {
Date date;
long gottimesss,millisecndslong;
@Override
public void onClick(View v) {
SimpleDateFormat displayFormat = new SimpleDateFormat("HH:mm");
SimpleDateFormat parseFormat = new SimpleDateFormat("hh:mm a");
try {
date = parseFormat.parse(hours.getText().toString() + ":" + minutes.getText().toString() + " " + ampm.getText().toString());
String gottime = displayFormat.format(date);
String[] timedivided = gottime.split(":");
String gothr = timedivided[0];
long gotlong = TimeUnit.HOURS.toMinutes(Long.parseLong(gothr));
String gotmin = timedivided[1];
int gotintmin = Integer.parseInt(gotmin);
gottimesss=gotlong+gotintmin;
millisecndslong=TimeUnit.MINUTES.toMillis(gottimesss);
convertSecToHoursMinute(millisecndslong);
Log.d("GOtssss", String.valueOf(millisecndslong));
Log.d("timing", "");
} catch (ParseException e) {
e.printStackTrace();
}
edittime.putLong("savedwakeuptime", millisecndslong);
edittime.commit();
startActivity(new Intent(getApplicationContext(), Home.class));
}
});
}
String convertSecToHoursMinute(long Sec) {
long hours = Sec / 3600;
long minutes = (Sec % 3600) / 60;
long seconds = (Sec % 3600) % 60;
String amPm = "am";
if (minutes == 60) {
minutes = 0;
hours = hours + 1;
}
if (hours == 12) {
amPm = "pm";
}
if (hours == 0) {
hours = 12;
}
if (hours > 12) {
hours = hours - 12;
amPm = "pm";
}
Log.d("Timingdata",setZeroBeforeNine(hours) + ":" + setZeroBeforeNine(minutes) + " " + amPm);
return setZeroBeforeNine(hours) + ":" + setZeroBeforeNine(minutes) + " " + amPm;
}
public static String setZeroBeforeNine(long digit) {
try {
if (digit <= 9) {
return "0" + digit;
}
} catch (Exception e) {
e.printStackTrace();
}
return "" + digit;
}
public static String roundedmin(String min) {
if (min.length() == 1) {
min = "0" + min;
}
return min;
}
}
【问题讨论】:
-
你试过什么?如果您显示您编写的代码不起作用,那么您将有更好的运气获得有关 stackoverflow 的帮助。
-
请出示您的代码。它将大大澄清您的问题。
-
相反,我猜 :-) [如果没有上午/下午信息,顺便说一句,这是不可能完成的]
-
我们在其中传递 am/pm。例如,当我们将时间设置为晚上 9 点时,从晚上 9 点到晚上 11:59 需要正确的时间,然后将上午 12:00 作为“0”值,而从上午 12:00 到上午 7:00 则不需要时间并在那里给出负值。
标签: java android time simpledateformat