Java Code Examples for java.util.Calendar.getDisplayName()

The following are Jave code examples for showing how to use getDisplayName() of the java.util.Calendar class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: openjdk-jdk10   File: Bug6448234.java   View Source Code Vote up 9 votes
public static void main(String[] args) {
    Calendar jcal = Calendar.getInstance(new Locale("ja", "JP", "JP"));
    Calendar gcal = Calendar.getInstance(Locale.US);

    for (int i = SUNDAY; i <= SATURDAY; i++) {
        jcal.set(DAY_OF_WEEK, i);
        gcal.set(DAY_OF_WEEK, i);

        // Test LONG
        String j = jcal.getDisplayName(DAY_OF_WEEK, LONG, Locale.US);
        String g = gcal.getDisplayName(DAY_OF_WEEK, LONG, Locale.US);
        if (!j.equals(g)) {
            throw new RuntimeException("Got " + j + ", expected " + g);
        }

        // Test SHORT
        j = jcal.getDisplayName(DAY_OF_WEEK, SHORT, Locale.US);
        g = gcal.getDisplayName(DAY_OF_WEEK, SHORT, Locale.US);
        if (!j.equals(g)) {
            throw new RuntimeException("Got " + j + ", expected " + g);
        }
    }
}
 
Example 2
Project: AssistantBySDK   File: DayPickerView.java   View Source Code Vote up 7 votes
private static String getMonthAndYearString(MonthAdapter.CalendarDay day) {
    Calendar cal = Calendar.getInstance();
    cal.set(day.year, day.month, day.day);

    String sbuf = "";
    sbuf += cal.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault());
    sbuf += " ";
    sbuf += YEAR_FORMAT.format(cal.getTime());
    return sbuf;
}
 
Example 3
Project: logistimo-web-service   File: AssetBuilder.java   View Source Code Vote up 7 votes
private String getTemperatureLabel(long time, String timezone) {
  Calendar calendar = Calendar.getInstance();
  calendar.setTimeZone(TimeZone.getTimeZone(timezone));
  calendar.setTimeInMillis(time * 1000);
  return calendar.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.getDefault())
      + " " + String.format("%02d", calendar.get(Calendar.DAY_OF_MONTH))
      + " " + String.format("%02d", calendar.get(Calendar.HOUR_OF_DAY))
      + ":" + String.format("%02d", calendar.get(Calendar.MINUTE));
}
 
Example 4
Project: HabitTracker   File: MainActivity.java   View Source Code Vote up 7 votes
/**
 * Add Habit object to either fulfilledHabits or unfulfilledHabits. Consider the habit finished
 * within today as a recent completed habit.
 * @param habit Habit object.
 * @param today Today.
 */
private void addToHabits(Habit habit, Calendar today) {
    boolean notPast = false;
    boolean isToday = false;
    boolean shouldOccur = false;
    Calendar habitDate = habit.getHabitDate();
    // https://www.tutorialspoint.com/java/util/calendar_getdisplaynames.htm
    Locale locale = Locale.CANADA;
    String todayOfWeek = today.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, locale);
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy MMM dd");

    if (today.after(habitDate)) {
        notPast = true;
    }
    if (dateFormat.format(today.getTime()).equals(habitDate)) {
        isToday = true;
    }
    if (habit.getOccurDays().contains(todayOfWeek)) {
        shouldOccur = true;
    }

    if ((notPast || isToday) && shouldOccur) {
        if (habit.getFulfilDate().isEmpty()) {
            unfulfilledHabits.add(habit);
        } else {
            for (Calendar date : habit.getFulfilDate()) {
                // if the habit has been fulfilled today
                if (dateFormat.format(date.getTime()).equals(dateFormat.format(today.getTime()))) {
                    fulfilledHabits.add(habit);
                    break;
                }
            }
        }
    }
}
 
Example 5
Project: letv   File: StringUtils.java   View Source Code Vote up 5 votes
private static String getWeekName(Calendar calendar) {
    String name = calendar.getDisplayName(7, 1, Locale.CHINESE);
    if (TextUtils.isEmpty(name)) {
        return "";
    }
    return name.replace(LetvUtils.getString(R.string.week_xinqi), LetvUtils.getString(R.string.week_zhou));
}
 
Example 6
Project: boohee_v5.6   File: DayPickerView.java   View Source Code Vote up 5 votes
private static String getMonthAndYearString(CalendarDay day) {
    Calendar cal = Calendar.getInstance();
    cal.set(day.year, day.month, day.day);
    return (("" + cal.getDisplayName(2, 2, Locale.getDefault())) + " ") + YEAR_FORMAT.format
            (cal.getTime());
}
 
Example 7
Project: pair-trading-for-android   File: GoogleFinanceDownloader.java   View Source Code Vote up 5 votes
String datetimeToStr(Calendar c){
    String month = c.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.US);
    return  month+"+"+c.get(Calendar.DAY_OF_MONTH)+"%2C+" + c.get(Calendar.YEAR);
}
 
Example 8
Project: incubator-netbeans   File: SchedulingPickerImpl.java   View Source Code Vote up 4 votes
@NbBundle.Messages({
    "# {0} - date from", "# {1} - date until", "CTL_SpecificDates={0} - {1}"
})
private static String toDisplayable (IssueScheduleInfo info) {
    Date date = info.getDate();
    int interval = info.getInterval();
    Calendar cal = Calendar.getInstance();
    stripTime(cal);
    Calendar scheduleDay = Calendar.getInstance();
    scheduleDay.setTime(date);
    stripTime(scheduleDay);
    
    if (interval <= 1) {
        String dayName = scheduleDay.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault());

        for (int i = 0; i < 7; ++i) {
            if (cal.getTime().equals(scheduleDay.getTime())) {
                if (i == 0) {
                    dayName = Bundle.CTL_Today(dayName);
                }
                return dayName;
            }
            cal.add(Calendar.DATE, 1);
        }
        return DATE_FORMAT.format(date);
    } else {
        scheduleDay.setTime(date);
        stripTime(scheduleDay);
        Calendar until = Calendar.getInstance();
        until.setTime(date);
        stripTime(until);
        until.add(Calendar.DATE, interval);
        
        if (isThisWeek(scheduleDay, until)) {
            return Bundle.CTL_ThisWeek();
        } else if (isNextWeek(scheduleDay, until)) {
            return Bundle.CTL_NextWeek();
        } else {
            return Bundle.CTL_SpecificDates(DATE_FORMAT.format(scheduleDay.getTime()),
                    DATE_FORMAT.format(until.getTime()));
        }
    }
}
 
Example 9
Project: android_ui   File: MonthView.java   View Source Code Vote up 4 votes
/**
 * Obtains a name of a calendar field with the specified <var>field</var> identifier from the
 * given <var>calendar</var> for the specified <var>locale</var>.
 * <p>
 * <b>Note</b>, that for pre {@link android.os.Build.VERSION_CODES#GINGERBREAD GINGERBREAD} Android
 * version and field names listed below, this method will return only English name regardless
 * the specified <var>locale</var>:
 * <ul>
 * <li>{@link Calendar#MONTH}</li>
 * <li>{@link Calendar#DAY_OF_WEEK}</li>
 * </ul>
 *
 * @param calendar The calendar from which to obtain the requested field's name.
 * @param field    The desired field identifier. See {@link Calendar} for field identifiers.
 * @param style    Style flag for the requested name. One of {@link #CALENDAR_STYLE_SHORT} or
 *                 {@link #CALENDAR_STYLE_LONG}.
 * @param locale   The locale for which to obtain the requested name. If there is no name available
 *                 for the requested locale, {@link UiConfig#DEFAULT_LOCALE} will be used instead.
 * @return Name of the requested calendar field.
 */
@NonNull
@SuppressLint("NewApi")
public static String resolveCalendarFieldName(@NonNull Calendar calendar, int field, int style, @NonNull Locale locale) {
	if (!PRE_GINGERBREAD) {
		final String name = calendar.getDisplayName(field, style, locale);
		return name != null ? name : calendar.getDisplayName(field, style, UiConfig.DEFAULT_LOCALE);
	}
	switch (field) {
		case Calendar.MONTH:
			return PRE_GINGERBREAD_MONTH_NAMES[calendar.get(field)];
		case Calendar.DAY_OF_WEEK:
			return PRE_GINGERBREAD_DAY_NAMES[calendar.get(field) - 1];
	}
	return "";
}
 
Example 10
Project: True-Weather   File: ShowWeather.java   View Source Code Vote up 4 votes
public void addForcastUI(final WeahterDay dayItem) {
    int screenW = Resources.getSystem().getDisplayMetrics().widthPixels;
    int itemW = screenW / 4;

    View layout = LayoutInflater.from(this).inflate(R.layout.sub_item_layout, null);

    TextView subDayTv = (TextView) layout.findViewById(R.id.subDayTv);
    ImageView subIconTv = (ImageView) layout.findViewById(R.id.subIconTv);
    TextView subTempMaxTv = (TextView) layout.findViewById(R.id.subTempMaxTv);
    TextView subTempMinTv = (TextView) layout.findViewById(R.id.subTempMinTv);

    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(dayItem.getTimestmp());
    String dayOfWeek = cal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.ENGLISH);
    subDayTv.setText(dayOfWeek);
    subTempMaxTv.setText("↑" + String.format("%.0f", dayItem.getTempMax()) + "°");
    subTempMinTv.setText("↓" + String.format("%.0f", dayItem.getTempMin()) + "°");

    subIconTv.getLayoutParams().height = itemW;
    subIconTv.setImageResource(dayItem.getIconRes());

    final MyData md = new MyData();
    final long[] dt = new long[dayItem.getItems().size()];
    int i = 0;
    for (WeatherItem wi : dayItem.getItems()) {
        md.add((float) wi.getTemp());
        //md.add(wi.getTimestmp(), (float) wi.getTemp());
        dt[i++] = wi.getTimestmp();
    }

    layout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showMainDetails(dayItem);
            showGraph(md, dt);
            showFullDetails(dayItem);
            showMap();
        }
    });

    forcastLL.addView(layout);

}
 
Example 11
Project: javaportfolio   File: MonthlyProfitsPane.java   View Source Code Vote up 4 votes
private Object[][] getTable(String portfolioName, int year) {

		Calendar current = Calendar.getInstance();
		Calendar c = Calendar.getInstance();
		
		
		int nbrofmonths;
		if (year == current.get(Calendar.YEAR)) {
			nbrofmonths = Math.min(12, current.get(Calendar.MONTH));
		}
		else {
			nbrofmonths = 12;
		}
		Object[][] table = new Object[nbrofmonths][columnNames.length];
		int row = 0;
		Portfolio portfolio = PortfolioFactory.createPortfolio(portfolioName,
				current.getTime(), TickerManager
						.createTickerManager());
		
		for (int month = 0; month < nbrofmonths; ++month) {
			try {

				c.set(Calendar.YEAR, year);
				c.set(Calendar.MONTH, month);
				c.set(Calendar.DATE, 1);
				Date startDate = c.getTime();
				
				portfolio.process(startDate);
				portfolio.getCombinedBookEntryTable(false);
				double value1 = portfolio.getPortfolioValue();

				
				c.set(Calendar.DAY_OF_MONTH,
		                c.getActualMaximum(Calendar.DAY_OF_MONTH));
				
				Date endDate = null;
								
				if (c.get(Calendar.YEAR) != Calendar.getInstance().get(
						Calendar.YEAR) || c.get(Calendar.MONTH) != Calendar.getInstance().get(
								Calendar.MONTH)) {
					endDate = c.getTime();
				} else {
					endDate = (Calendar.getInstance().getTime());
				}
				
				portfolio.process(endDate);
				portfolio.getCombinedBookEntryTable(false);
				double value2 = portfolio.getPortfolioValue();

				int col = 0;
				table[row][col++] = c.getDisplayName(Calendar.MONTH, Calendar.LONG, getLocale());
				table[row][col++] = value1;
				table[row][col++] = value2;
				table[row][col++] = value2 - value1;
			    table[row][col++] = (value2 - value1)/value1*100.0;
				//table[row][col++] = portfolio.getXirr(value1, startDate);
				row++;
			} catch (NumberFormatException e) {

			}
		}
		return table;

	}
 
Example 12
Project: RDF2PT   File: LiteralConverterPortuguese.java   View Source Code Vote up 4 votes
private String convertDateLiteral(LiteralLabel lit) {
	RDFDatatype dt = lit.getDatatype();
	String s = lit.getLexicalForm();

	try {
		Object value = lit.getValue();
		if (value instanceof XSDDateTime) {
			Calendar calendar = ((XSDDateTime) value).asCalendar();
			if (dt.equals(XSDDatatype.XSDgMonth)) {
				s = calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, PORTUGUESE_LOCAL);
			} else if (dt.equals(XSDDatatype.XSDgMonthDay)) {
				s = calendar.get(Calendar.DAY_OF_MONTH)
						+ calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, PORTUGUESE_LOCAL);
			} else if (dt.equals(XSDDatatype.XSDgYearMonth)) {
				s = calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, PORTUGUESE_LOCAL) + " "
						+ calendar.get(Calendar.YEAR);
			} else if (dt.equals(XSDDatatype.XSDgYear)) {
				s = "" + calendar.get(Calendar.YEAR);
			} else {
				s = calendar.get(Calendar.DAY_OF_MONTH) + " de "
						+ calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, PORTUGUESE_LOCAL) + " de "
						+ calendar.get(Calendar.YEAR);
				// dateFormat.format(calendar.getTime());
			}
		}
	} catch (DatatypeFormatException | IllegalDateTimeFieldException e) {
		logger.error("Conversion of date literal " + lit + " failed. Reason: " + e.getMessage());
		// fallback
		// DateTime time = ISODateTimeFormat.dateTime
		DateTime time;
		try {
			time = ISODateTimeFormat.dateTimeParser().parseDateTime(lit.getLexicalForm());
			s = time.toString("dd 'de' MMMM 'de' YYYY", PORTUGUESE_LOCAL);
		} catch (Exception e1) {
			try {
				time = ISODateTimeFormat.localDateParser().parseDateTime(lit.getLexicalForm());
				s = time.toString("dd 'de' MMMM 'de' YYYY", PORTUGUESE_LOCAL);
			} catch (Exception e2) {
				e2.printStackTrace();
				time = ISODateTimeFormat.dateParser().parseDateTime(lit.getLexicalForm());
				s = time.toString("dd 'de' MMMM 'de' YYYY", PORTUGUESE_LOCAL);
			}
		}
	}
	return s;
}
 
Example 13
Project: RDF2PT   File: LiteralConverter.java   View Source Code Vote up 4 votes
private String convertDateLiteral(LiteralLabel lit){
    	RDFDatatype dt = lit.getDatatype();
    	String s = lit.getLexicalForm();
    	
    	try {
			Object value = lit.getValue();
			if (value instanceof XSDDateTime) {
				Calendar calendar = ((XSDDateTime) value).asCalendar();
				if(dt.equals(XSDDatatype.XSDgMonth)){
					s = calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, ENGLISH_LOCAL);
				} else if(dt.equals(XSDDatatype.XSDgMonthDay)){
					s = calendar.get(Calendar.DAY_OF_MONTH) + calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, ENGLISH_LOCAL);
				} else if(dt.equals(XSDDatatype.XSDgYearMonth)) {
					s = calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, ENGLISH_LOCAL) + " " + calendar.get(Calendar.YEAR);
				} else if(dt.equals(XSDDatatype.XSDgYear)) {
					s = "" + calendar.get(Calendar.YEAR);
				} else {
					s = calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, ENGLISH_LOCAL) + " " + calendar.get(Calendar.DAY_OF_MONTH)
					+ ", " + calendar.get(Calendar.YEAR);
					// dateFormat.format(calendar.getTime());
				}
			}
		} catch (DatatypeFormatException | IllegalDateTimeFieldException e) {
			logger.error("Conversion of date literal " + lit + " failed. Reason: " + e.getMessage());
			//fallback
//			DateTime time = ISODateTimeFormat.dateTime
			DateTime time;
			try {
				time = ISODateTimeFormat.dateTimeParser().parseDateTime(lit.getLexicalForm());
				s = time.toString("MMMM dd, yyyy");
			} catch (Exception e1) {
				try {
					time = ISODateTimeFormat.localDateParser().parseDateTime(lit.getLexicalForm());
					s = time.toString("MMMM dd, yyyy");
				} catch (Exception e2) {
					e2.printStackTrace();
					time = ISODateTimeFormat.dateParser().parseDateTime(lit.getLexicalForm());
					s = time.toString("MMMM dd, yyyy");
				}
			}
		}
    	return s;
    }
 
Example 14
Project: RDF2PT   File: LiteralConverterFrench.java   View Source Code Vote up 4 votes
private String convertDateLiteral(LiteralLabel lit) {
	RDFDatatype dt = lit.getDatatype();
	String s = lit.getLexicalForm();

	try {
		Object value = lit.getValue();
		if (value instanceof XSDDateTime) {
			Calendar calendar = ((XSDDateTime) value).asCalendar();
			if (dt.equals(XSDDatatype.XSDgMonth)) {
				s = calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, FRENCH_LOCAL);
			} else if (dt.equals(XSDDatatype.XSDgMonthDay)) {
				s = calendar.get(Calendar.DAY_OF_MONTH)
						+ calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, FRENCH_LOCAL);
			} else if (dt.equals(XSDDatatype.XSDgYearMonth)) {
				s = calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, FRENCH_LOCAL) + " "
						+ calendar.get(Calendar.YEAR);
			} else if (dt.equals(XSDDatatype.XSDgYear)) {
				s = "" + calendar.get(Calendar.YEAR);
			} else {
				s = calendar.get(Calendar.DAY_OF_MONTH) + " "
						+ calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, FRENCH_LOCAL) + " "
						+ calendar.get(Calendar.YEAR);
				// dateFormat.format(calendar.getTime());
			}
		}
	} catch (DatatypeFormatException | IllegalDateTimeFieldException e) {
		logger.error("Conversion of date literal " + lit + " failed. Reason: " + e.getMessage());
		// fallback
		// DateTime time = ISODateTimeFormat.dateTime
		DateTime time;
		try {
			time = ISODateTimeFormat.dateTimeParser().parseDateTime(lit.getLexicalForm());
			s = time.toString("dd MMMM YYYY", FRENCH_LOCAL);
		} catch (Exception e1) {
			try {
				time = ISODateTimeFormat.localDateParser().parseDateTime(lit.getLexicalForm());
				s = time.toString("dd MMMM YYYY", FRENCH_LOCAL);
			} catch (Exception e2) {
				e2.printStackTrace();
				time = ISODateTimeFormat.dateParser().parseDateTime(lit.getLexicalForm());
				s = time.toString("dd MMMM YYYY", FRENCH_LOCAL);
			}
		}
	}
	return s;
}