private String _getCurrentTimestamp() { return new SimpleDateFormat("k:m:s:S a", Locale.getDefault()).format(new Date()); } }
public class CustomCalendarSerializer extends JsonSerializer<Calendar> { public static final SimpleDateFormat FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm"); public static final Locale LOCALE_HUNGARIAN = new Locale("hu", "HU"); public static final TimeZone LOCAL_TIME_ZONE = TimeZone.getTimeZone("Europe/Budapest"); @Override public void serialize(Calendar value, JsonGenerator gen, SerializerProvider arg2) throws IOException, JsonProcessingException { if (value == null) { gen.writeNull(); } else { gen.writeString(FORMATTER.format(value.getTime())); } } }
/** * Resolves locale code from locale. */ public static String resolveLocaleCode(Locale locale) { return resolveLocaleCode(locale.getLanguage(), locale.getCountry(), locale.getVariant()); }
/** * Determine the RFC 3066 compliant language tag, * as used for the HTTP "Accept-Language" header. * @param locale the Locale to transform to a language tag * @return the RFC 3066 compliant language tag as {@code String} * @deprecated as of 5.0.4, in favor of {@link Locale#toLanguageTag()} */ @Deprecated public static String toLanguageTag(Locale locale) { return locale.getLanguage() + (hasText(locale.getCountry()) ? "-" + locale.getCountry() : ""); }
/** * Render the given locale as a text value for inclusion in a cookie. * <p>The default implementation calls {@link Locale#toString()} * or JDK 7's {@link Locale#toLanguageTag()}, depending on the * {@link #setLanguageTagCompliant "languageTagCompliant"} configuration property. * @param locale the locale to stringify * @return a String representation for the given locale * @since 4.3 * @see #isLanguageTagCompliant() */ protected String toLocaleValue(Locale locale) { return (isLanguageTagCompliant() ? locale.toLanguageTag() : locale.toString()); }
/** * This test case validates date pattern before and after DST * Base Date : 12 Mar 2017 * Daylight Savings started on : 02:00 AM */ @Test public void testFormatLong_goingBackInTime_DST() { final Calendar instance = Calendar.getInstance(TimeZone.getTimeZone("EST")); instance.set(2017, 2, 12, 2, 0); final long now = instance.getTimeInMillis(); final long start = now - TimeUnit.HOURS.toMillis(1); final long end = now + TimeUnit.HOURS.toMillis(1); for (final FixedFormat format : FixedFormat.values()) { if (format.getPattern().endsWith("n")) { continue; // cannot compile precise timestamp formats with SimpleDateFormat } final SimpleDateFormat simpleDF = new SimpleDateFormat(format.getPattern(), Locale.getDefault()); final FixedDateFormat customTF = new FixedDateFormat(format, TimeZone.getDefault()); for (long time = end; time > start; time -= 12345) { final String actual = customTF.format(time); final String expected = simpleDF.format(new Date(time)); assertEquals(format + "(" + format.getPattern() + ")" + "/" + time, expected, actual); } } } }
public Date getTimestamp() { try { // Parse the timestamp into a Date object return new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.getDefault()) .parse(timestamp); } catch (IllegalArgumentException | ParseException e) { // Return current Date return new Date(); } }
private String getDateTime() { SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss", Locale.getDefault()); Date date = new Date(); return dateFormat.format(date); } ContentValues values = new ContentValues(); values.put('username', 'ravitamada'); values.put('created_at', getDateTime()); // insert the row long id = db.insert('users', null, values);
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.getDefault()); Date currentLocalTime = calendar.getTime(); DateFormat date = new SimpleDateFormat("Z"); String localTime = date.format(currentLocalTime);
private void validateSdfFormatFdpParseEquality(final String format, final Locale locale, final TimeZone tz, final DateParser fdp, final Date in, final int year, final Date cs) throws ParseException { final SimpleDateFormat sdf = new SimpleDateFormat(format, locale); sdf.setTimeZone(tz); if (format.equals(SHORT_FORMAT)) { sdf.set2DigitYearStart( cs ); } final String fmt = sdf.format(in); try { final Date out = fdp.parse(fmt); assertEquals(locale.toString()+" "+in+" "+ format+ " "+tz.getID(), in, out); } catch (final ParseException pe) { if (year >= 1868 || !locale.getCountry().equals("JP")) {// LANG-978 throw pe; } } }
public class Timeis { public static void main(String s[]) { long ts = 1022895271767L; SimpleDateFormat sdf = new SimpleDateFormat(" MMM d 'at' hh:mm a"); // CREATE DateFormatSymbols WITH ALL SYMBOLS FROM (DEFAULT) Locale DateFormatSymbols symbols = new DateFormatSymbols(Locale.getDefault()); // OVERRIDE SOME symbols WHILE RETAINING OTHERS symbols.setAmPmStrings(new String[] { "am", "pm" }); sdf.setDateFormatSymbols(symbols); String st = sdf.format(ts); System.out.println("time is " + st); } }
private static String getMonthAndYearString(CalendarDay day) { Calendar cal = Calendar.getInstance(); cal.set(day.year, day.month, day.day); StringBuffer sbuf = new StringBuffer(); sbuf.append(cal.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault())); sbuf.append(" "); sbuf.append(YEAR_FORMAT.format(cal.getTime())); return sbuf.toString(); }
private void testLocales(final String format, final boolean eraBC) throws Exception { final Calendar cal= Calendar.getInstance(GMT); cal.clear(); cal.set(2003, Calendar.FEBRUARY, 10); if (eraBC) { cal.set(Calendar.ERA, GregorianCalendar.BC); } for(final Locale locale : Locale.getAvailableLocales() ) { // ja_JP_JP cannot handle dates before 1868 properly if (eraBC && locale.equals(FastDateParser.JAPANESE_IMPERIAL)) { continue; } final SimpleDateFormat sdf = new SimpleDateFormat(format, locale); final DateParser fdf = getInstance(format, locale); try { checkParse(locale, cal, sdf, fdf); } catch(final ParseException ex) { fail("Locale "+locale+ " failed with "+format+" era "+(eraBC?"BC":"AD")+"\n" + trimMessage(ex.toString())); } } }
/** * Gets the check message 'as is' from appropriate 'messages.properties' * file. * * @param messageBundle the bundle name. * @param messageKey the key of message in 'messages.properties' file. * @param arguments the arguments of message in 'messages.properties' file. * @return The message of the check with the arguments applied. */ private static String internalGetCheckMessage( String messageBundle, String messageKey, Object... arguments) { final ResourceBundle resourceBundle = ResourceBundle.getBundle( messageBundle, Locale.getDefault(), Thread.currentThread().getContextClassLoader(), new LocalizedMessage.Utf8Control()); final String pattern = resourceBundle.getString(messageKey); final MessageFormat formatter = new MessageFormat(pattern, Locale.ROOT); return formatter.format(arguments); }
String getStartDateTime() { if (startDateTime == null) { SimpleDateFormat format = new SimpleDateFormat( "EEE, d MMM yyyy HH:mm:ss z", new Locale("en", "")); format.setTimeZone(DateTimeUtils.UTC); startDateTime = format.format(System.currentTimeMillis()); } return startDateTime; }
private void updateDisplay(boolean announce) { /*if (mDayOfWeekView != null) { mDayOfWeekView.setText(mCalendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault()).toUpperCase(Locale.getDefault())); } mSelectedMonthTextView.setText(mCalendar.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.getDefault()).toUpperCase(Locale.getDefault()));*/ if (this.mDayOfWeekView != null){ this.mCalendar.setFirstDayOfWeek(mWeekStart); this.mDayOfWeekView.setText(mDateFormatSymbols.getWeekdays()[this.mCalendar.get(Calendar.DAY_OF_WEEK)].toUpperCase(Locale.getDefault())); } this.mSelectedMonthTextView.setText(mDateFormatSymbols.getMonths()[this.mCalendar.get(Calendar.MONTH)].toUpperCase(Locale.getDefault())); mSelectedDayTextView.setText(DAY_FORMAT.format(mCalendar.getTime())); mYearView.setText(YEAR_FORMAT.format(mCalendar.getTime())); // Accessibility. long millis = mCalendar.getTimeInMillis(); mAnimator.setDateMillis(millis); int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NO_YEAR; String monthAndDayText = DateUtils.formatDateTime(getActivity(), millis, flags); mMonthAndDayView.setContentDescription(monthAndDayText); if (announce) { flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR; String fullDateText = DateUtils.formatDateTime(getActivity(), millis, flags); Utils.tryAccessibilityAnnounce(mAnimator, fullDateText); } }
private String generateLog () { StringBuilder mEventLog = new StringBuilder(); setTitle("Event @ " + mEvent.getStartTime().toLocaleString()); for (EventTrigger eventTrigger : mEvent.getEventTriggers()) { mEventLog.append("Event Triggered @ ").append( new SimpleDateFormat(Utils.DATE_TIME_PATTERN, Locale.getDefault()).format(eventTrigger.getTriggerTime())).append("\n"); String sType = eventTrigger.getStringType(this); mEventLog.append("Event Type: ").append(sType); mEventLog.append("\n==========================\n"); } return mEventLog.toString(); }
locale = Locale.getDefault(); ResourceBundle rb = null; if (null == (rb = (ResourceBundle) threadMap.get(locale.toString()))) { rb = ResourceBundle.getBundle("javax.el.PrivateMessages", locale); threadMap.put(locale.toString(), rb); result = rb.getString(messageId); if (null != params) { result = MessageFormat.format(result, params);
private void updateDisplay(boolean announce) { if (mDayOfWeekView != null) { mDayOfWeekView.setText(mCalendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault()).toUpperCase(Locale.getDefault())); } mSelectedMonthTextView.setText(mCalendar.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.getDefault()).toUpperCase(Locale.getDefault())); mSelectedDayTextView.setText(DAY_FORMAT.format(mCalendar.getTime())); mYearView.setText(YEAR_FORMAT.format(mCalendar.getTime())); // Accessibility. long millis = mCalendar.getTimeInMillis(); mAnimator.setDateMillis(millis); int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NO_YEAR; String monthAndDayText = DateUtils.formatDateTime(getActivity(), millis, flags); mMonthAndDayView.setContentDescription(monthAndDayText); if (announce) { flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR; String fullDateText = DateUtils.formatDateTime(getActivity(), millis, flags); Utils.tryAccessibilityAnnounce(mAnimator, fullDateText); } }
throws MissingResourceException Locale locale = Locale.getDefault(); return (ListResourceBundle)ResourceBundle.getBundle(className, locale); return (ListResourceBundle)ResourceBundle.getBundle( className, new Locale("en", "US")); throw new MissingResourceException( "Could not load any resource bundles." + className, className, "");