Example usage for java.util Calendar getDisplayName

List of usage examples for java.util Calendar getDisplayName

Introduction

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

Prototype

public String getDisplayName(int field, int style, Locale locale) 

Source Link

Document

Returns the string representation of the calendar field value in the given style and locale.

Usage

From source file:eu.squadd.timesheets.eolas.TimeTemplate.java

public String prepareTimesheet(String[] args) {
    String response = null;/*from   w  w  w. j  a v a  2 s .  c  o  m*/
    try {
        String[] ym = args[0].split("/");
        month = Integer.parseInt(ym[0]);
        year = Integer.parseInt(ym[1]);

        Calendar cal = Calendar.getInstance(TimeZone.getDefault());
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        monthName = cal.getDisplayName(Calendar.MONTH, Calendar.SHORT_FORMAT, Locale.ENGLISH);
        String periodName = monthName + "-" + year;
        cal.set(Calendar.DATE, 1);
        String dayOfWeek = new SimpleDateFormat("EE").format(cal.getTime());

        System.out.println("Month: " + periodName);
        System.out.println("Days in month: " + days);
        System.out.println("Month starts in: " + dayOfWeek);

        Map<String, String> bankHolidays = year == 2016 ? publicHolidays2016 : publicHolidays2017;
        Map<String, String> holidays = this.extractHolidays(args);

        HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(template));
        HSSFSheet sheet = wb.getSheet("timesheet"); //getSheetAt(0);
        HSSFRow currentRow;
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
        sheet.getRow(4).getCell(1).setCellValue(periodName);
        int row = 7;
        int startRow = 0;
        int i = 1;
        while (i <= days) {
            currentRow = sheet.getRow(row);
            if (currentRow.getRowNum() > 47)
                break;
            String day = currentRow.getCell(0).getStringCellValue();

            if (day.startsWith("Total")) {
                evaluator.evaluateFormulaCell(currentRow.getCell(2));
                evaluator.evaluateFormulaCell(currentRow.getCell(4));
                row++;
                continue;
            }

            if (startRow == 0) {
                if (dayOfWeek.equals(day.substring(0, 3))) {
                    startRow = currentRow.getRowNum();
                    System.out.println("Starting row found: " + startRow + 1);
                } else {
                    row++;
                    continue;
                }
            }
            cal.set(Calendar.DATE, i);
            String date = sdf.format(cal.getTime());
            if (!day.equals("Saturday") && !day.equals("Sunday") && bankHolidays.get(date) == null
                    && holidays.get(date) == null) {
                currentRow.getCell(1).setCellValue(date);
                currentRow.getCell(2).setCellValue(defaultHours); // regular hours
                //currentRow.getCell(3).setCellValue(defaultHours);   // overtime hours
                currentRow.getCell(4).setCellValue(defaultHours); // total hours                    
            }
            i++;
            row++;
        }
        currentRow = sheet.getRow(46);
        evaluator.evaluateFormulaCell(currentRow.getCell(2));
        evaluator.evaluateFormulaCell(currentRow.getCell(4));
        currentRow = sheet.getRow(47);
        evaluator.evaluateFormulaCell(currentRow.getCell(2));
        evaluator.evaluateFormulaCell(currentRow.getCell(4));
        response = outFilePath.replace("#MONTH#", periodName);
        wb.write(new FileOutputStream(response));

    } catch (IOException ex) {
        Logger.getLogger(Timesheets.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.out.println("Timesheet created.");
    return response;
}

From source file:de.linuxwhatelse.android.notify.activities.MainActivity.java

private void handleSnoozedNotification() {
    this.initChannels(this, getString(R.string.notification_channel_snooze));

    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);

    boolean notificationsSnoozed = preferences.getBoolean(Notify.PREFERENCE_KEY_NOTIFICATIONS_SNOOZED, false);
    boolean eventsSnoozed = preferences.getBoolean(Notify.PREFERENCE_KEY_EVENTS_SNOOZED, false);
    long snoozeUntil = preferences.getLong(Notify.PREFERENCE_KEY_SNOOZED_UNTIL, -1);

    if (!notificationsSnoozed && !eventsSnoozed) {
        notificationManager.cancel(Notify.NOTIFICATION_ID_SNOOZE);
        return;/*from   ww  w  .j  av  a 2s .com*/
    }

    String title = getString(R.string.snoozed_notifications_title);
    if (notificationsSnoozed && eventsSnoozed) {
        title = getString(R.string.snoozed_all_title);
    }

    String body = getString(R.string.snoozed_text_resume_indefinitely);
    if (snoozeUntil != -1) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(snoozeUntil);
        String day = cal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.getDefault());
        String time = (new SimpleDateFormat("HH:mm", getResources().getConfiguration().locale))
                .format(cal.getTime());

        body = getString(R.string.snoozed_text_resume_at) + " " + day + " " + time;
    }

    Intent activityIntent = new Intent(context, MainActivity.class);
    activityIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent activityPendingIntent = PendingIntent.getActivity(context, 0, activityIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, Notify.NOTIFICATION_CHANNEL)
            .setContentTitle(title).setContentText(body).setSmallIcon(R.drawable.ic_notifications_paused)
            .setPriority(NotificationCompat.PRIORITY_LOW).setContentIntent(activityPendingIntent)
            .setOngoing(true);

    Intent intent = new Intent(getApplicationContext(), SnoozeEndReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.ic_notifications_active,
            getString(R.string.snoozed_action_resume), pendingIntent).build();
    mBuilder.addAction(action);

    notificationManager.notify(Notify.NOTIFICATION_ID_SNOOZE, mBuilder.build());
}

From source file:com.hacktx.android.activities.EventDetailActivity.java

private void setupEventDetails() {
    int eventIconId;
    switch (event.getType()) {
    case TALK:/* ww w.  ja  v  a2 s.  c  o m*/
        eventIconId = R.drawable.ic_event_talk;
        break;
    case EDUCATION:
        eventIconId = R.drawable.ic_event_education;
        break;
    case BUS:
        eventIconId = R.drawable.ic_event_bus;
        break;
    case FOOD:
        eventIconId = R.drawable.ic_event_food;
        break;
    case DEV:
        eventIconId = R.drawable.ic_event_dev;
        break;
    default:
        eventIconId = R.drawable.ic_event_default;
        break;
    }

    ImageView eventIcon = (ImageView) findViewById(R.id.eventIcon);
    eventIcon.setImageResource(eventIconId);

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
    Calendar start = Calendar.getInstance();

    try {
        start.setTime(formatter.parse(event.getStartDate()));
    } catch (ParseException e) {
        e.printStackTrace();
    }

    ((TextView) findViewById(R.id.eventLocation)).setText(event.getLocation().getLocationDetails());
    ((TextView) findViewById(R.id.eventDateTime))
            .setText(start.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault()) + " "
                    + start.get(Calendar.DAY_OF_MONTH) + " | " + event.getEventTimes());
}

From source file:org.botlibre.util.Utils.java

public static String displayDate(Date date) {
    if (date == null) {
        return "";
    }//from   w  w w.java  2  s  . c o  m
    StringWriter writer = new StringWriter();
    Calendar today = Calendar.getInstance();
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    if (calendar.get(Calendar.YEAR) == today.get(Calendar.YEAR)
            && calendar.get(Calendar.DAY_OF_YEAR) == today.get(Calendar.DAY_OF_YEAR)) {
        writer.write("Today");
    } else if (calendar.get(Calendar.YEAR) == today.get(Calendar.YEAR)
            && calendar.get(Calendar.DAY_OF_YEAR) == (today.get(Calendar.DAY_OF_YEAR) - 1)) {
        writer.write("Yesterday");
    } else {
        writer.write(calendar.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.US));
        writer.write(" ");
        writer.write(String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)));
        if (calendar.get(Calendar.YEAR) != today.get(Calendar.YEAR)) {
            writer.write(" ");
            writer.write(String.valueOf(calendar.get(Calendar.YEAR)));
        }
    }

    return writer.toString();
}

From source file:org.botlibre.util.Utils.java

public static String displayTimestamp(Date date) {
    if (date == null) {
        return "";
    }/* www.ja va2 s  .c  o  m*/
    StringWriter writer = new StringWriter();
    Calendar today = Calendar.getInstance();
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    if (calendar.get(Calendar.YEAR) == today.get(Calendar.YEAR)
            && calendar.get(Calendar.DAY_OF_YEAR) == today.get(Calendar.DAY_OF_YEAR)) {
        writer.write("Today");
    } else if (calendar.get(Calendar.YEAR) == today.get(Calendar.YEAR)
            && calendar.get(Calendar.DAY_OF_YEAR) == (today.get(Calendar.DAY_OF_YEAR) - 1)) {
        writer.write("Yesterday");
    } else {
        writer.write(calendar.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.US));
        writer.write(" ");
        writer.write(String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)));
        if (calendar.get(Calendar.YEAR) != today.get(Calendar.YEAR)) {
            writer.write(" ");
            writer.write(String.valueOf(calendar.get(Calendar.YEAR)));
        }
    }
    writer.write(", ");
    writer.write(String.valueOf(calendar.get(Calendar.HOUR_OF_DAY)));
    writer.write(":");
    if (calendar.get(Calendar.MINUTE) < 10) {
        writer.write("0");
    }
    writer.write(String.valueOf(calendar.get(Calendar.MINUTE)));

    return writer.toString();
}

From source file:com.kunze.androidlocaltodo.TaskListActivity.java

private void SetFriendlyDueDateText(TextView dueDateView, Calendar dueDate) {
    SimpleDateFormat dateFormatDisplay = new SimpleDateFormat("MM-dd-yyyy", Locale.US);
    Calendar now = Calendar.getInstance();
    int nowDay = TaskDatabase.ConvertDateToInt(now);
    int dueDay = TaskDatabase.ConvertDateToInt(dueDate);
    int dayDiff = nowDay - dueDay;
    if (dayDiff == 0) {
        dueDateView.setText("Today");
        dueDateView.setTextColor(Color.RED);
    } else if (dueDate.before(now)) {
        dueDateView.setText("+ " + dayDiff + " days!");
        dueDateView.setTextColor(Color.RED);
    } else if (dayDiff > -7) {
        dueDateView.setText(dueDate.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.US));
        dueDateView.setTextColor(Color.BLACK);
    } else if (dayDiff > -14) {
        dueDateView.setText("Next " + dueDate.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.US));
        dueDateView.setTextColor(Color.BLACK);
    } else {// w w w.  j  a v a  2 s.  c o m
        dueDateView.setText(dateFormatDisplay.format(dueDate.getTime()));
        dueDateView.setTextColor(Color.BLACK);
    }
}

From source file:com.miz.functions.MizLib.java

public static String getPrettyDate(Context context, String date) {
    if (!TextUtils.isEmpty(date)) {
        try {/*from  w ww .j  av a  2 s  .  c o  m*/
            String[] dateArray = date.split("-");
            Calendar cal = Calendar.getInstance();
            cal.set(Integer.parseInt(dateArray[0]), Integer.parseInt(dateArray[1]) - 1,
                    Integer.parseInt(dateArray[2]));

            return MizLib
                    .toCapitalFirstChar(cal.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault())
                            + " " + cal.get(Calendar.YEAR));
        } catch (Exception e) { // Fall back if something goes wrong
            return date;
        }
    } else {
        return context.getString(R.string.stringNA);
    }
}

From source file:com.antsdb.saltedfish.sql.vdm.FuncDateFormat.java

private String format(String format, Timestamp time) {
    StringBuilder buf = new StringBuilder();
    for (int i = 0; i < format.length(); i++) {
        char ch = format.charAt(i);
        if (ch != '%') {
            buf.append(ch);/*  w w w.j a  v a 2s  . c om*/
            continue;
        }
        if (i >= (format.length() - 1)) {
            buf.append(ch);
            continue;
        }
        char specifier = format.charAt(++i);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time.getTime());
        if (specifier == 'a') {
            throw new NotImplementedException();
        } else if (specifier == 'b') {
            throw new NotImplementedException();
        } else if (specifier == 'c') {
            buf.append(calendar.get(Calendar.MONTH + 1));
        } else if (specifier == 'd') {
            int day = calendar.get(Calendar.DAY_OF_MONTH);
            if (day < 10) {
                buf.append('0');
            }
            buf.append(day);
        } else if (specifier == 'D') {
            throw new NotImplementedException();
        } else if (specifier == 'e') {
            buf.append(calendar.get(Calendar.DAY_OF_MONTH));
        } else if (specifier == 'f') {
            buf.append(calendar.get(Calendar.MILLISECOND * 1000));
        } else if (specifier == 'H') {
            buf.append(calendar.get(Calendar.HOUR));
        } else if (specifier == 'h') {
            buf.append(calendar.get(Calendar.HOUR) % 13);
        } else if (specifier == 'i') {
            buf.append(calendar.get(Calendar.MINUTE));
        } else if (specifier == 'I') {
            buf.append(calendar.get(Calendar.HOUR) % 13);
        } else if (specifier == 'j') {
            buf.append(calendar.get(Calendar.DAY_OF_YEAR));
        } else if (specifier == 'k') {
            buf.append(calendar.get(Calendar.HOUR));
        } else if (specifier == 'l') {
            buf.append(calendar.get(Calendar.HOUR) % 13);
        } else if (specifier == 'm') {
            int month = calendar.get(Calendar.MONTH) + 1;
            if (month < 10) {
                buf.append('0');
            }
            buf.append(calendar.get(Calendar.MONTH) + 1);
        } else if (specifier == 'M') {
            buf.append(calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault()));
        } else if (specifier == 'p') {
            int hour = calendar.get(Calendar.HOUR);
            buf.append(hour < 12 ? "AM" : "PM");
        } else if (specifier == 'r') {
            int hour = calendar.get(Calendar.HOUR);
            hour = hour % 13;
            if (hour < 10) {
                buf.append('0');
            }
            buf.append(hour);
            buf.append(':');
            int minute = calendar.get(Calendar.MINUTE);
            if (minute < 10) {
                buf.append('0');
            }
            buf.append(minute);
            buf.append(':');
            int second = calendar.get(Calendar.SECOND);
            if (second < 10) {
                buf.append('0');
            }
            buf.append(second);
            buf.append(hour < 12 ? " AM" : " PM");
        } else if (specifier == 's') {
            buf.append(calendar.get(Calendar.SECOND));
        } else if (specifier == 'S') {
            buf.append(calendar.get(Calendar.SECOND));
        } else if (specifier == 'T') {
            throw new NotImplementedException();
        } else if (specifier == 'u') {
            buf.append(calendar.get(Calendar.WEEK_OF_YEAR));
        } else if (specifier == 'U') {
            throw new NotImplementedException();
        } else if (specifier == 'v') {
            throw new NotImplementedException();
        } else if (specifier == 'V') {
            throw new NotImplementedException();
        } else if (specifier == 'w') {
            throw new NotImplementedException();
        } else if (specifier == 'W') {
            throw new NotImplementedException();
        } else if (specifier == 'x') {
            throw new NotImplementedException();
        } else if (specifier == 'X') {
            throw new NotImplementedException();
        } else if (specifier == 'y') {
            buf.append(calendar.get(Calendar.YEAR) % 100);
        } else if (specifier == 'Y') {
            buf.append(calendar.get(Calendar.YEAR));
        } else if (specifier == '%') {
            buf.append('%');
        } else {
            buf.append(specifier);
        }
    }
    return buf.toString();
}