有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓中的Calendar class set()方法让java感到困惑

 Calendar cal = Calendar.getInstance();
            cal.set(Calendar.YEAR,Integer.parseInt(YYYY));
            cal.set(Calendar.MONTH, Integer.parseInt(MM));
            cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(DD));
            cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hh));
            cal.set(Calendar.MINUTE, Integer.parseInt(mm));
            cal.set(Calendar.SECOND,00);
            SimpleDateFormat simpledateformat = new SimpleDateFormat("dd-MM-yyyy hh:mm");
             EditText check = (EditText) findViewById(R.id.check);
             check.setText(simpledateformat.format(cal.getTime()));

这就是我设置2018年11月28日下午2点56分AlarmManager发送意向的时间,但我一点击发送按钮,它就会发送意向

意图代码

Intent sendIntent = getPackageManager().getLaunchIntentForPackage("com.whatsapp");
            try {
                PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0,sendIntent,PendingIntent.FLAG_ONE_SHOT);
                ((AlarmManager) getSystemService(ALARM_SERVICE)).setExact(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),pendingIntent);
            }
            catch (安卓.content.ActivityNotFoundException ex) {
                Toast.makeText(MainActivity.this,"Please Install Whatsapp Messenger", Toast.LENGTH_LONG).show();
            }

时区为UTC+5:30

我是否将时间设定在过去,以及如何纠正它

编辑:我用编辑文本显示时间,2018年11月29日变成15-06-0211。怎么做

编辑:也许这是错的

 EditText date = (EditText) findViewById(R.id.date);
            String dd = date.getText().toString();

            char c[] = dd.toCharArray();
            String YYYY = c[6]+c[7]+c[8]+c[9]+"";
            String MM = c[3]+c[4]+"";
            String DD =c[0]+c[1]+"";

答案-使用子字符串方法。但我想知道为什么上述方法是错误的


共 (2) 个答案

  1. # 1 楼答案

    爪哇。时间

        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd-MM-uuuu");
        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("h:mm a");
        ZoneId zone = ZoneId.of("Asia/Kolkata");
    
        String dateString = "29-11-2018";
        String timeString = "2:56 PM";
        LocalDate alarmDate = LocalDate.parse(dateString, dateFormatter);
        LocalTime alarmTime = LocalTime.parse(timeString, timeFormatter);
        long triggerAlarmAtMillis = alarmDate.atTime(alarmTime)
                .atZone(zone)
                .toInstant()
                .toEpochMilli();
    
        System.out.println("Alarm will happen at millis " + triggerAlarmAtMillis);
    

    输出为:

    Alarm will happen at millis 1543483560000

    您可以使用在线历元时间转换器来验证这是否与格林尼治标准时间(GMT:周四29)一致。2018年11月09:26:00,与偏移量+05:30时的14:56相同。链接在底部

    考虑避免使用日历和SimpleDateFormat

    您使用的日期时间类存在严重的设计问题,被认为早已过时。我建议你使用java。而是时间。如果您的Android API级别需要,那么通过ThreeTenABP,请参见下文

    我还建议您将日期和时间解析留给库方法。无论是选择单个字符还是子字符串,这都是一种困难且容易出错的方法

    But I want to know why above method is wrong?

    这是一个丑陋而令人惊讶的java事实,在很多地方{{CD1>}和^ {< CD2}}可以互换使用(这已经被C++及其前驱所取代)。例如:

        String MM = c[3]+c[4]+"";
    

    c[3]c[4]都是1,您原本以为这会给出字符串"11"。表达式是从左到右计算的,因此添加了int个字符的等价物1表示为数字49。49+49等于98。将98添加到空字符串中时,它首先会转换为字符串本身(如预期的那样),因此得到字符串"98"。同样,对于日期,你得到50+57=107,对于年份50+48+49+56=203

    下一个惊喜(我想)。虽然将a Calendar的年份设置为203可能有意义,但将月份设置为98应该是一个错误并导致异常,对吗?不适用于带有默认设置的Calendar类。它只是在203年之后的几年里持续数月,直到211年结束。同样,当你将月日设置为107时,它会持续计算接下来几个月的天数,并在6月15日(Sat Jun 15 211)结束——这仍然是1800多年前的事了

    然而,你的代码中还有一个bug。这里:

        cal.set(Calendar.MONTH, Integer.parseInt(MM));
    

    即使MM按计划11,这也不会将月份设置为11月。关于Calendar的另一件令人困惑的事情是,月份数字是以0为基础的:0是一月,以此类推,所以11是十二月,而不是十一月

    问题:我可以在Android上使用java.time

    是的,java.time在较旧和较新的安卓设备上运行良好。它只需要至少Java6

    • 在Java8和更高版本中,以及在新的Android设备上(我听说是API级别26),新的API是内置的
    • 在Java6和Java7中,获取三个后端口,即新类的后端口(JSR310的三个后端口,最早描述现代API的地方)
    • 在(旧版)Android上,使用Android版的Three Ten Backport。它叫ThreeTenABP。确保从包org.threeten.bp和子包导入日期和时间类

    链接

  2. # 2 楼答案

    您正在从EditText中提取错误的值
    首先将格式从:

    "dd-MM-yyyy hh:mm"
    

    "dd-MM-yyyy HH:mm"
    

    因为你的格式是12小时制而不是24小时制
    然后使用这些值:

    String DD = dd.substring(0, 2);
    String MM = dd.substring(3, 5);
    String YYYY = dd.substring(6, 10);
    String hh = dd.substring(11, 13);
    String mm = dd.substring(14, 16);