我要在除周末以外的两个日期之间取得差额,但我想在一天结束时获取上午时间。因为如果工作人员在下午开始工作,那么我想半天增加许可日期。 ?

private void Calculation() {
    final SimpleDateFormat sdf = new SimpleDateFormat("h:mm a");
    Date d3 = null;
    Date d4 = null;

    try {
        d3 = sdf.parse(izinSaatim); //String izinSaatim value parse to date
    } catch (ParseException e) {
        e.printStackTrace();
    }
    try {
        d4 = sdf.parse(basSaatim);String basSaatim value parse to date
    } catch (ParseException e) {
        e.printStackTrace();
    }
    Calendar c3 = Calendar.getInstance();
    c3.setTime(d3);
    Calendar c4 = Calendar.getInstance();
    c3.setTime(d4);
    if(d3.getTime()>=Calendar.AM || d4.getTime()>=Calendar.AM){
      //....
    }
}



  两个日期之间的差异:


  private void hesapla() {

        final SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
        Date d1 = null;
        Date d2 = null;
        try {
            d1 = format.parse(izinTarihim);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        try {
            d2 = format.parse(basTarihim);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar c1 = Calendar.getInstance();
        c1.setTime(d1);
        int w1 = c1.get(Calendar.DAY_OF_WEEK);
        c1.add(Calendar.DAY_OF_WEEK, -w1);

        Calendar c2 = Calendar.getInstance();
        c2.setTime(d2);
        int w2 = c2.get(Calendar.DAY_OF_WEEK);
        c2.add(Calendar.DAY_OF_WEEK, -w2);

        long days = (c2.getTimeInMillis() - c1.getTimeInMillis()) / (1000 * 60 * 60 * 24);
        long daysWithoutWeekendDays = days - (days * 2 / 7);
        if (w1 == Calendar.SUNDAY && w2 != Calendar.SATURDAY) {
            w1 = Calendar.MONDAY;
        } else if (w1 == Calendar.SATURDAY && w2 != Calendar.SUNDAY) {
            w1 = Calendar.FRIDAY;
        }

        if (w2 == Calendar.SUNDAY) {
            w2 = Calendar.MONDAY;
        } else if (w2 == Calendar.SATURDAY) {
            w2 = Calendar.FRIDAY;
        }
        long a = daysWithoutWeekendDays - w1 + w2;

        gunSayi = Long.toString(a);

        kullanilan.setText(gunSayi);
    }

最佳答案

用这个

 SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yy hh.mm.ss.S aa");
 String formattedDate = dateFormat.format(new Date()).toString();
 System.out.println(formattedDate);


您将获得05年8月11日下午12.25.15.375

关于java - 如何在Timepicker中选择上午或下午时间?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40176300/

10-09 19:22