如何在Timepicker中选择a.m或p.m时间?

时间:2016-10-21 12:07:03

标签: java android calendar

我在两个日期之间得到区别,不包括周末。但是我想在一天结束时得到时间AM。因为如果工作人员在下午开始工作,那么我想在下半天添加许可。我该怎么办?

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

2 个答案:

答案 0 :(得分:2)

使用此

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

您将获得 11-Aug-05 12.25.15.375 PM

答案 1 :(得分:1)

如果您想从Calander对象获取AM或PM,请执行以下代码

Calendar now = Calendar.getInstance();
int a = now.get(Calendar.AM_PM);
if(a == Calendar.AM)
     //the time is PM . do necessary actions
else
     // time is PM . Do neccessary actions