查找特定日期的天数,小时数和分钟数

时间:2014-05-02 21:34:49

标签: java android date calendar

我是java开发的新手,遇到了我需要帮助的问题。

我需要找出直到早上8点30分的日子(如适用),小时和分钟。然而,由于这是一个“倒计时学校”,代码必须知道,如果下一个上午8点30分是在星期六或星期日,它应该显示时间,直到星期一上午8点30分。再次,抱歉,如果这没有多大意义。所以我接近这个的逻辑方式是:

  1. 确定明天是星期一,星期二,星期三,星期四还是星期五。
  2. 如果是,请计算明天上午8点30分之前的小时数和分钟数,并将其转换成2个字符串(小时和分钟)
  3. 否则,计算一天,几小时和几分钟,直到星期一上午8点30分,并将它们转换成字符串(天,小时和分钟)

    Date dt = new Date();
    Calendar c = Calendar.getInstance();
    c.setTime(dt);
    c.set(Calendar.HOUR_OF_DAY, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);
    dt = c.getTime();
    DateFormat dateFormat = new SimpleDateFormat("dd");
    String datetommorow = dateFormat.format(dt);
    int date22 = Integer.parseInt(datetommorow);
    int date33 = date22 + 1;
    Integer.toString(date33);
    
  4. 从这次尝试中我可以看出,我正在努力解决这个问题。任何帮助将不胜感激,如果您需要更多信息请不要犹豫

3 个答案:

答案 0 :(得分:1)

        Calendar now = Calendar.getInstance();
        int currentDay = now.get(Calendar.DAY_OF_WEEK);

        Calendar school = Calendar.getInstance();
        school.add(Calendar.DAY_OF_YEAR, 1);

        if (currentDay == Calendar.SATURDAY)
        {
            school.add(Calendar.DAY_OF_YEAR, 1);
        }
        else if (currentDay == Calendar.FRIDAY)
        {
            school.add(Calendar.DAY_OF_YEAR, 2);
        }

        school.set(Calendar.HOUR_OF_DAY, 8);
        school.set(Calendar.MINUTE, 30);

        long millisLeft = school.getTimeInMillis() - now.getTimeInMillis();
        long hoursLeft = millisLeft / (60 * 60 * 1000);
        long minutesLeft =  (millisLeft % (60 * 60 * 1000)) / (60 * 1000);

答案 1 :(得分:0)

这应该可以帮到你

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, 1);
        SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
        System.out.println("Tommorow is : " + sdf.format(cal.getTime()));

        Calendar future = Calendar.getInstance(); //future time
        future.add(Calendar.DAY_OF_MONTH, 1);
        future.set(Calendar.HOUR_OF_DAY,8); // Set hours to 8'O clock
        future.set(Calendar.MINUTE,30);
        Calendar now = Calendar.getInstance(); //get current time
        long hoursDiff = (future.getTimeInMillis() - now.getTimeInMillis())/(60 * 60 * 1000);
        long minDiff = (future.getTimeInMillis() - now.getTimeInMillis())/(60 * 1000);
        System.out.println("Hours left : " + hoursDiff);
        System.out.println("Minutes left : " + minDiff);

答案 2 :(得分:0)

您可能需要查看适用于Android的Joda-Time。此外,DateFormat在多线程环境中本质上是不安全的,并且Date API一般都是丑陋的(至少对于< = 1.7 afaik)