Example usage for java.util Calendar AM_PM

List of usage examples for java.util Calendar AM_PM

Introduction

In this page you can find the example usage for java.util Calendar AM_PM.

Prototype

int AM_PM

To view the source code for java.util Calendar AM_PM.

Click Source Link

Document

Field number for get and set indicating whether the HOUR is before or after noon.

Usage

From source file:TimeUtil.java

public static String stringSecsFormat(long msecs) {
    GregorianCalendar cal = new GregorianCalendar();
    StringBuffer sBuf = new StringBuffer(11);

    cal.setTime(new Date(msecs));

    int hour = cal.get(Calendar.HOUR);

    if (hour == 0)
        hour = 12;//  ww  w.  ja v a 2s .  co  m

    if (hour < 10)
        sBuf.append(" ");

    sBuf.append(Integer.toString(hour));
    sBuf.append(":");

    int minute = cal.get(Calendar.MINUTE);

    if (minute < 10)
        sBuf.append("0");

    sBuf.append(Integer.toString(minute));
    sBuf.append(":");

    int secs = cal.get(Calendar.SECOND);

    if (secs < 10) {
        sBuf.append("0");
    }
    sBuf.append(Integer.toString(secs));

    sBuf.append(" ");
    sBuf.append(cal.get(Calendar.AM_PM) == Calendar.AM ? "AM" : "PM");

    return (sBuf.toString());
}

From source file:TimeUtil.java

/**
 * convert time in milliseconds into a display string of the form [h]h:mm
 * [am|pm] (traditional) or hh:mm (24 hour format) if using traditional
 * format, the leading 'h' & 'm' will be padded with a space to ensure
 * constant length if less than 10 24 hour format
 * // ww  w .j a va2  s.  com
 * @param msecs
 *            a millisecond time
 * @return TimeString the formatted time string
 */
public static String stringFormat(long msecs) {
    GregorianCalendar cal = new GregorianCalendar();
    StringBuffer sBuf = new StringBuffer(8);

    cal.setTime(new Date(msecs));

    int hour = cal.get(Calendar.HOUR);

    if (hour == 0)
        hour = 12;

    if (hour < 10)
        sBuf.append(" ");

    sBuf.append(Integer.toString(hour));
    sBuf.append(":");

    int minute = cal.get(Calendar.MINUTE);

    if (minute < 10)
        sBuf.append("0");

    sBuf.append(Integer.toString(minute));
    sBuf.append(" ");
    sBuf.append(cal.get(Calendar.AM_PM) == Calendar.AM ? "AM" : "PM");

    return (sBuf.toString());
}

From source file:CalendarDemo.java

public void format() {

    // Tell the calendar what date/time to format
    calendar.setTime(timeNow);//from w  w w.  j ava2  s  . com

    // print out most of the known fields
    System.out.println("ERA: " + calendar.get(Calendar.ERA));
    System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
    System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
    System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
    System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
    System.out.println("DATE: " + calendar.get(Calendar.DATE));
    System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
    System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
    System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
    System.out.println("DAY_OF_WEEK_IN_MONTH: " + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
    System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
    System.out.println("HOUR: " + calendar.get(Calendar.HOUR));
    System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));
    System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));
    System.out.println("SECOND: " + calendar.get(Calendar.SECOND));
    System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
    System.out.println("ZONE_OFFSET: " + (calendar.get(Calendar.ZONE_OFFSET) / (60 * 60 * 1000)));
    System.out.println("DST_OFFSET: " + (calendar.get(Calendar.DST_OFFSET) / (60 * 60 * 1000)));
}

From source file:org.kalypso.commons.time.PeriodUtils.java

public static Period getPeriod(final int calendarField, final int amount) {
    switch (calendarField) {
    case Calendar.YEAR:
        return Period.years(amount);

    case Calendar.MONTH:
        return Period.months(amount);

    case Calendar.WEEK_OF_YEAR:
    case Calendar.WEEK_OF_MONTH:
        return Period.weeks(amount);

    case Calendar.DAY_OF_MONTH:
    case Calendar.DAY_OF_YEAR:
    case Calendar.DAY_OF_WEEK:
    case Calendar.DAY_OF_WEEK_IN_MONTH:
        return Period.days(amount);

    case Calendar.HOUR:
    case Calendar.HOUR_OF_DAY:
        return Period.hours(amount);

    case Calendar.MINUTE:
        return Period.minutes(amount);

    case Calendar.SECOND:
        return Period.seconds(amount);

    case Calendar.MILLISECOND:
        return Period.millis(amount);

    case Calendar.AM_PM:
    case Calendar.ERA:
    default://from   w w w.  j  a va  2s . com
        throw new UnsupportedOperationException();
    }
}

From source file:org.primefaces.extensions.showcase.controller.TimePickerController.java

public TimePickerController() {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.AM_PM, Calendar.AM);
    calendar.set(Calendar.HOUR, 8);
    calendar.set(Calendar.MINUTE, 15);
    time1 = calendar.getTime();/*from w w w . j a va  2 s  . c om*/

    calendar.set(Calendar.HOUR, 11);
    calendar.set(Calendar.MINUTE, 40);
    time2 = calendar.getTime();

    time3 = new Date();

    time5 = new Date();

    time6 = new Date();
}

From source file:com.brayanarias.alarmproject.receiver.AlarmReceiver.java

private static void setAlarmCumple(Context context) {
    PendingIntent pendingIntent = null;/*www  .j a  va  2s .c  o  m*/
    Intent intent = new Intent(context, AlarmService.class);
    intent.putExtra(Constant.alarmIdKey, 2809);
    intent.putExtra(Constant.alarmNameKey, "Feliz cumple mamita");
    pendingIntent = PendingIntent.getService(context, 2809, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = getAlarmManager(context);
    Calendar actual = Calendar.getInstance();
    actual.set(Calendar.MONTH, 8);
    actual.set(Calendar.DAY_OF_MONTH, 28);
    actual.set(Calendar.SECOND, 0);
    actual.set(Calendar.MILLISECOND, 0);
    actual.set(Calendar.HOUR, 5);
    actual.set(Calendar.AM_PM, Calendar.AM);
    actual.set(Calendar.MINUTE, 0);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        AlarmManager.AlarmClockInfo clockInfo = new AlarmManager.AlarmClockInfo(actual.getTimeInMillis(),
                pendingIntent);
        alarmManager.setAlarmClock(clockInfo, pendingIntent);
    } else if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, actual.getTimeInMillis(), pendingIntent);
    } else {
        alarmManager.set(AlarmManager.RTC_WAKEUP, actual.getTimeInMillis(), pendingIntent);
    }
}

From source file:helper.util.DateHelper.java

/**
 * Take a local time calendar and represent it as a day
 * (truncate to day, retain yy/mm/dd from original date)
 **///from  ww w .j  av a 2 s .  c o  m
public static Calendar asDay(Calendar localTimeCalendar) {
    Calendar asDay = Calendar.getInstance(localTimeCalendar.getTimeZone());
    asDay.clear();
    asDay.set(localTimeCalendar.get(Calendar.YEAR), localTimeCalendar.get(Calendar.MONTH),
            localTimeCalendar.get(Calendar.DATE), 0, 0, 0);
    asDay.set(Calendar.AM_PM, Calendar.AM);
    asDay.set(Calendar.MILLISECOND, 0);
    asDay.set(Calendar.HOUR, 0);
    return asDay;
}

From source file:org.osaf.cosmo.scheduler.ForwardLookingNotificationJob.java

@Override
protected Report generateReport(JobExecutionContext context) throws JobExecutionException {

    TimeZone tz = timezone == null ? null : TimeZoneUtils.getTimeZone(timezone);
    Locale loc = locale == null ? Locale.getDefault() : new Locale(locale);

    // start date is the start of the day of the fire time

    Calendar cal = new GregorianCalendar();
    if (tz != null)
        cal.setTimeZone(tz);/*from   w  ww  . j  a  v a  2s.c  o m*/

    cal.setTime(context.getFireTime());
    cal.set(Calendar.AM_PM, Calendar.AM);
    cal.set(Calendar.HOUR, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);

    Date startDate = new Date(cal.getTime().getTime());

    // End date is either 1 day or 1 week later
    // depending on report type
    if (REPORT_TYPE_DAILY.equals(reportType))
        cal.add(Calendar.DAY_OF_YEAR, 1);
    else
        cal.add(Calendar.WEEK_OF_YEAR, 1);

    cal.add(Calendar.SECOND, -1);

    Date endDate = cal.getTime();

    if (log.isDebugEnabled()) {
        log.debug("startDate=" + startDate);
        log.debug("endDate = " + endDate);
    }

    // get results
    List<UpcomingResult> upcomingItems = new ArrayList<UpcomingResult>();
    List<NowResult> nowItems = new ArrayList<NowResult>();

    Iterator<String> it = getCollectionUids().iterator();
    while (it.hasNext()) {
        String colUid = it.next();

        // We have to handle changes to underlying collections.
        // A collection could have been deleted, or access to the
        // collection has been removed. In both cases, remove the
        // collection from the list so that the next job run
        // won't attempt to access it.
        try {
            getCollectionResults(colUid, startDate, endDate, tz, upcomingItems, nowItems);
        } catch (ItemSecurityException ise) {
            // user is not authorized for collection,
            // remove collection for next execution
            log.info("user " + getUsername() + " not authorized for collection " + colUid
                    + ", removing from schedule " + context.getJobDetail().getName());
            it.remove();
        } catch (ItemNotFoundException infe) {
            // collection not found, remove collection for next execution
            log.info("collection " + colUid + " not found removing from schedule " + getUsername() + ":"
                    + context.getJobDetail().getName());
            it.remove();
        }
    }

    // sort results
    Comparator<UpcomingResult> comparator = new UpcomingResultComparator(false, tz);
    Collections.sort(upcomingItems, comparator);

    // return results
    ForwardLookingReport report = new ForwardLookingReport(getUser());
    report.setStartDate(startDate);
    report.setTimezone(tz == null ? TimeZone.getDefault() : tz);
    report.setLocale(loc);
    report.setReportType(reportType);
    report.setUpcomingItems(upcomingItems);
    report.setNowItems(nowItems);
    return report;
}

From source file:org.sakaiproject.tool.section.jsf.backingbean.LocalMeetingModel.java

public LocalMeetingModel(Meeting meeting) {
    this.location = meeting.getLocation();
    this.monday = meeting.isMonday();
    this.tuesday = meeting.isTuesday();
    this.wednesday = meeting.isWednesday();
    this.thursday = meeting.isThursday();
    this.friday = meeting.isFriday();
    this.saturday = meeting.isSaturday();
    this.sunday = meeting.isSunday();
    this.startTimeString = JsfUtil.convertTimeToString(meeting.getStartTime());
    if (startTimeString == null) {
        startTimeString = JsfUtil.getLocalizedMessage("section_start_time_default");
    }//from  w  w w  . ja  v a 2s  . c  om

    this.endTimeString = JsfUtil.convertTimeToString(meeting.getEndTime());
    if (endTimeString == null) {
        endTimeString = JsfUtil.getLocalizedMessage("section_end_time_default");
    }

    Calendar cal = new GregorianCalendar();
    if (meeting.getStartTime() == null) {
        this.startTimeAm = true;
    } else {
        cal.setTime(meeting.getStartTime());
        if (log.isDebugEnabled())
            log.debug("cal.get(Calendar.AM_PM) = " + cal.get(Calendar.AM_PM));
        this.startTimeAm = (cal.get(Calendar.AM_PM) == Calendar.AM);
    }
    if (meeting.getEndTime() == null) {
        this.endTimeAm = true;
    } else {
        cal.setTime(meeting.getEndTime());
        if (log.isDebugEnabled())
            log.debug("cal.get(Calendar.AM_PM) = " + cal.get(Calendar.AM_PM));
        this.endTimeAm = (cal.get(Calendar.AM_PM) == Calendar.AM);
    }
}

From source file:DateUtils.java

public static final String getTimeFromDate(Date dt, String tzString) {
    try {// w  w  w .  j av  a 2 s.c  o  m
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(dt);
        gc.setTimeZone(TimeZone.getTimeZone(tzString));
        StringBuffer ret = new StringBuffer();
        ret.append(gc.get(Calendar.HOUR));
        ret.append(":");
        ret.append(gc.get(Calendar.MINUTE));
        ret.append(" ");
        if (gc.get(Calendar.AM_PM) == 0) {
            ret.append("AM");
        } else {
            ret.append("PM");
        }
        return ret.toString();
    } catch (Exception e) {
        return "";
    }
}