@Override public String formatDate(Locale locale, Date date) { return DateFormat.getDateInstance(DateFormat.DEFAULT, locale).format(date); }
String getPattern(Locale locale) { DateFormat f = null; switch (iType) { case DATE: f = DateFormat.getDateInstance(iDateStyle, locale); break; case TIME: f = DateFormat.getTimeInstance(iTimeStyle, locale); break; case DATETIME: f = DateFormat.getDateTimeInstance(iDateStyle, iTimeStyle, locale); break; } if (f instanceof SimpleDateFormat == false) { throw new IllegalArgumentException("No datetime pattern for locale: " + locale); } return ((SimpleDateFormat) f).toPattern(); } }
Locale locale = Locale.getDefault(); SimpleDateFormat sdf = (SimpleDateFormat)DateFormat.getDateInstance(DateFormat.SHORT, locale ); String format = sdf.toPattern();
String currentDateTimeString = DateFormat.getDateInstance().format(new Date()); // textView is the TextView view that should display it textView.setText(currentDateTimeString);
DateFormat.getDateInstance().format(new Date(0)); DateFormat.getDateTimeInstance().format(new Date(0)); DateFormat.getTimeInstance().format(new Date(0));
public static void main(String[] args) throws Exception { for (Locale locale : Locale.getAvailableLocales()) { DateFormat df = getShortDateInstanceWithoutYears(locale); System.out.println(locale + ": " + df.format(new Date())); } } public static DateFormat getShortDateInstanceWithoutYears(Locale locale) { SimpleDateFormat sdf = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT, locale); sdf.applyPattern(sdf.toPattern().replaceAll("[^\\p{Alpha}]*y+[^\\p{Alpha}]*", "")); return sdf; }
@Override @SuppressWarnings("checkstyle:emptyblock") public Date deserialize(JsonElement element, Type arg1, JsonDeserializationContext context) { final String dateString = element.getAsString(); Date date = null; for (final String df : dateFormats) { try { date = new Date(); mSimpleDateFormat = new SimpleDateFormat(df); date.setTime(mSimpleDateFormat.parse(dateString).getTime()); return date; } catch (final ParseException e) { // swallow , will try next type of date format } } // default to default implementation try { return DateFormat.getDateInstance(DateFormat.DEFAULT).parse(dateString); } catch (final ParseException e) { throw new JsonParseException(e.getMessage(), e); } }
private DateFormat createDateFormat(Locale locale) { if (StringUtils.hasLength(this.pattern)) { return new SimpleDateFormat(this.pattern, locale); } if (this.iso != null && this.iso != ISO.NONE) { String pattern = ISO_PATTERNS.get(this.iso); if (pattern == null) { throw new IllegalStateException("Unsupported ISO format " + this.iso); } SimpleDateFormat format = new SimpleDateFormat(pattern); format.setTimeZone(UTC); return format; } if (StringUtils.hasLength(this.stylePattern)) { int dateStyle = getStylePatternForChar(0); int timeStyle = getStylePatternForChar(1); if (dateStyle != -1 && timeStyle != -1) { return DateFormat.getDateTimeInstance(dateStyle, timeStyle, locale); } if (dateStyle != -1) { return DateFormat.getDateInstance(dateStyle, locale); } if (timeStyle != -1) { return DateFormat.getTimeInstance(timeStyle, locale); } throw new IllegalStateException("Unsupported style pattern '" + this.stylePattern + "'"); } return DateFormat.getDateInstance(this.style, locale); }
java.text.DateFormat.getDateInstance( java.text.DateFormat.SHORT, locale); final String dateValue = dateFormat.format(date); // "12/31/69" String dateSeparator = dateValue.substring(2, 3); // "/" java.text.DateFormat.getTimeInstance( java.text.DateFormat.SHORT, locale); final String timeValue = timeFormat.format(date); // "12:00:00" String timeSeparator = timeValue.substring(2, 3); // ":"
private String decodeSimpleDateFormat(StringBuffer buffer, Format format) { if (format.equals(DateFormat.getTimeInstance(DateFormat.DEFAULT, locale))) { buffer.append(",time"); } else if (format.equals(DateFormat.getDateInstance(DateFormat.DEFAULT, locale))) { buffer.append(",date"); } else if (format.equals(DateFormat.getTimeInstance(DateFormat.SHORT, locale))) { buffer.append(",time,short"); } else if (format.equals(DateFormat.getDateInstance(DateFormat.SHORT, locale))) { buffer.append(",date,short"); } else if (format.equals(DateFormat.getTimeInstance(DateFormat.LONG, locale))) { buffer.append(",time,long"); } else if (format.equals(DateFormat.getDateInstance(DateFormat.LONG, locale))) { buffer.append(",date,long"); } else if (format.equals(DateFormat.getTimeInstance(DateFormat.FULL, locale))) { buffer.append(",time,full"); } else if (format.equals(DateFormat.getDateInstance(DateFormat.FULL, locale))) { buffer.append(",date,full"); } else { buffer.append(",date,"); return ((SimpleDateFormat) format).toPattern(); } return null; }
case 1: // date if (ch == '}') { return type == 1 ? DateFormat.getDateInstance( DateFormat.DEFAULT, locale) : DateFormat .getTimeInstance(DateFormat.DEFAULT, locale); .getDateInstance(dateStyle, locale) : DateFormat .getTimeInstance(dateStyle, locale); case 2: // number if (ch == '}') {
import java.text.DateFormat; import java.util.Date; import java.util.Locale; public class DateFormatDemoSO { public static void main(String args[]) { int style = DateFormat.MEDIUM; //Also try with style = DateFormat.FULL and DateFormat.SHORT Date date = new Date(); DateFormat df; df = DateFormat.getDateInstance(style, Locale.UK); System.out.println("United Kingdom: " + df.format(date)); df = DateFormat.getDateInstance(style, Locale.US); System.out.println("USA: " + df.format(date)); df = DateFormat.getDateInstance(style, Locale.FRANCE); System.out.println("France: " + df.format(date)); df = DateFormat.getDateInstance(style, Locale.ITALY); System.out.println("Italy: " + df.format(date)); df = DateFormat.getDateInstance(style, Locale.JAPAN); System.out.println("Japan: " + df.format(date)); } }
dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale); } else if (type == FORMAT_TYPE.TIME) { dateFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale); } else { dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, locale); return dateFormat.format(date);
locales.add(Locale.CHINA); Date date = new Date(); SimpleDateFormat sdf = (SimpleDateFormat) SimpleDateFormat.getDateInstance(DateFormat.SHORT, l); String pattern = sdf.toPattern(); String localizedPattern = sdf.toLocalizedPattern() println "country: " + l.getDisplayName(); String dateString = df.format(date); println "resulting date: " + dateString String yearlessPattern = pattern.replaceAll("\\W?[Yy]+\\W?", ""); println "yearlessPattern = " + yearlessPattern; SimpleDateFormat yearlessSDF = new SimpleDateFormat(yearlessPattern, l); println "resulting date without year: " + yearlessSDF.format(date) + "\n";
@AfterViews void updateCaption() { DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG, Locale.US); String formattedDate = dateFormat.format(date); injectedTextView.setText(caption + " - " + formattedDate); }
String getPattern(Locale locale) { DateFormat f = null; switch (iType) { case DATE: f = DateFormat.getDateInstance(iDateStyle, locale); break; case TIME: f = DateFormat.getTimeInstance(iTimeStyle, locale); break; case DATETIME: f = DateFormat.getDateTimeInstance(iDateStyle, iTimeStyle, locale); break; } if (f instanceof SimpleDateFormat == false) { throw new IllegalArgumentException("No datetime pattern for locale: " + locale); } return ((SimpleDateFormat) f).toPattern(); } }
private DateFormat createDateFormat(Locale locale) { if (StringUtils.hasLength(this.pattern)) { return new SimpleDateFormat(this.pattern, locale); } if (this.iso != null && this.iso != ISO.NONE) { String pattern = ISO_PATTERNS.get(this.iso); if (pattern == null) { throw new IllegalStateException("Unsupported ISO format " + this.iso); } SimpleDateFormat format = new SimpleDateFormat(pattern); format.setTimeZone(UTC); return format; } if (StringUtils.hasLength(this.stylePattern)) { int dateStyle = getStylePatternForChar(0); int timeStyle = getStylePatternForChar(1); if (dateStyle != -1 && timeStyle != -1) { return DateFormat.getDateTimeInstance(dateStyle, timeStyle, locale); } if (dateStyle != -1) { return DateFormat.getDateInstance(dateStyle, locale); } if (timeStyle != -1) { return DateFormat.getTimeInstance(timeStyle, locale); } throw new IllegalStateException("Unsupported style pattern '" + this.stylePattern + "'"); } return DateFormat.getDateInstance(this.style, locale); }
public static String getJavaDatePattern(int style, Locale locale) { DateFormat df = DateFormat.getDateInstance(style, locale); if( df instanceof SimpleDateFormat ) { return ((SimpleDateFormat)df).toPattern(); } else { switch( style ) { case DateFormat.SHORT: return "d/MM/yy"; case DateFormat.MEDIUM: return "MMM d, yyyy"; case DateFormat.LONG: return "MMMM d, yyyy"; case DateFormat.FULL: return "dddd, MMMM d, yyyy"; default: return "MMM d, yyyy"; } } }
DateFormat df = null; if (type.equals("both")) { df = DateFormat.getDateInstance(getStyle(dateStyle), this.getLocale()); metadata.put(HTML.VALIDATION_METADATA.DATE_STYLE_PATTERN, CalendarUtils.convertPattern(((SimpleDateFormat) df).toPattern())); df = DateFormat.getTimeInstance(getStyle(timeStyle), this.getLocale()); metadata.put(HTML.VALIDATION_METADATA.TIME_STYLE_PATTERN, CalendarUtils.convertPattern(((SimpleDateFormat) df).toPattern())); df = DateFormat.getDateInstance(getStyle(dateStyle), this.getLocale()); metadata.put(HTML.VALIDATION_METADATA.DATE_STYLE_PATTERN, CalendarUtils.convertPattern(((SimpleDateFormat) df).toPattern())); df = DateFormat.getTimeInstance(getStyle(timeStyle), this.getLocale()); metadata.put(HTML.VALIDATION_METADATA.TIME_STYLE_PATTERN, CalendarUtils.convertPattern(((SimpleDateFormat) df).toPattern()));
public void initLog(File logFilePath) throws IOException { RedwoodConfiguration.empty() .handlers(RedwoodConfiguration.Handlers.chain( RedwoodConfiguration.Handlers.showAllChannels(), RedwoodConfiguration.Handlers.stderr), RedwoodConfiguration.Handlers.file(logFilePath.toString()) ).apply(); // fh.setFormatter(new NewlineLogFormatter()); System.out.println("Starting Ssurgeon log, at "+logFilePath.getAbsolutePath()+" date=" + DateFormat.getDateInstance(DateFormat.FULL).format(new Date())); log.info("Starting Ssurgeon log, date=" + DateFormat.getDateInstance(DateFormat.FULL).format(new Date())); }