将 12 小时转换为 24 小时

编程入门 行业动态 更新时间:2024-10-23 23:23:07
本文介绍了将 12 小时转换为 24 小时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

如何将12小时hh:mm a转换成HH:mm?当我们转换它时,我们无法进入 24 小时格式.当我们指定晚上 10 点开始时间和早上 7 点起床时间时,这里的时间格式 pm 到 am 没有转换.在这里我们无法获得总时间.它在中午 12:00 停止,而且我们从晚上 10 点到早上 7 点的总时间加倍为 18 小时.当时间从晚上 11:59 更改为上午 00:00 时00:00am" 导致这里出现问题.

How to convert 12 hours hh:mm a into HH:mm? When we converting this we are unable to get into 24 hours format. Here the time format pm to am is not converting when we assigned start time at 10pm and wake up time 7am. Here we are unable to get the total time. It's getting stopped at 12:00am and also we are getting the total time double from 10pm to 7am as 18 hours. when time is changing from 11:59pm to 00:00am "00:00am" is causing the problem here.

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);
edittimemit();
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;
}
}

推荐答案

使用这两种方法将秒转换为 24 小时格式 public static

Use these two method for convert second to 24hours format public static

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";
        }
        return setZeroBeforeNine(hours) + ":" + setZeroBeforeNine(minutes) + " " + amPm;
    }

    /**
     * To set Zero before digits if less than 9(for date and time)
     *
     * @param digit : Date and time before which 0 is placed if less than 9
     * @return :return Digit placing 0 as prefix if digit is less than 9
     */
    public static String setZeroBeforeNine(long digit) {
        try {
            if (digit <= 9) {
                return "0" + digit;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "" + digit;
    }

这篇关于将 12 小时转换为 24 小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 12:11:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1393555.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小时   转换为

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!