@Override protected DateFormat initialValue() { // Date format specified by RFC 7231 section 7.1.1.1. DateFormat rfc1123 = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US); rfc1123.setLenient(false); rfc1123.setTimeZone(UTC); return rfc1123; } };
@Override protected NumberFormat getNumberFormat(Locale locale) { NumberFormat format = NumberFormat.getPercentInstance(locale); if (format instanceof DecimalFormat) { ((DecimalFormat) format).setParseBigDecimal(true); } return format; }
/** * Create a MessageFormat for the given message and Locale. * @param msg the message to create a MessageFormat for * @param locale the Locale to create a MessageFormat for * @return the MessageFormat instance */ protected MessageFormat createMessageFormat(String msg, Locale locale) { return new MessageFormat(msg, locale); }
public static String toString(java.util.Date date) { if (date == null) { return null; } SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return format.format(date); }
String dt = "2008-01-01"; // Start date SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar c = Calendar.getInstance(); c.setTime(sdf.parse(dt)); c.add(Calendar.DATE, 1); // number of days to add dt = sdf.format(c.getTime()); // dt is now the new date
// Create an instance of SimpleDateFormat used for formatting // the string representation of date (month/day/year) DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); // Get the date today using Calendar object. Date today = Calendar.getInstance().getTime(); // Using DateFormat format method we can create a string // representation of a date with the defined format. String reportDate = df.format(today); // Print what date is today! System.out.println("Report Date: " + reportDate);
String oldstring = "2011-01-18 00:00:00.0"; Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(oldstring);
private DateFormat newDateFormat() { SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.US); dateFormat.setTimeZone(GMT); return dateFormat; }
/** * Define the date/time format with a {@link SimpleDateFormat}. * <p>Note: Setting this property makes the exposed {@link ObjectMapper} * non-thread-safe, according to Jackson's thread safety rules. * @see #dateFormat(DateFormat) */ public Jackson2ObjectMapperBuilder simpleDateFormat(String format) { this.dateFormat = new SimpleDateFormat(format); return this; }
public String formatMessage(Object... inserts) { return MessageFormat.format(this.message, inserts); } }
private Date getStartDate() throws ParseException { Date start = df.parse("2004/10/12"); return start; }
public static boolean isNumeric(String str) { NumberFormat formatter = NumberFormat.getInstance(); ParsePosition pos = new ParsePosition(0); formatter.parse(str, pos); return str.length() == pos.getIndex(); }
@Override protected NumberFormat getNumberFormat(Locale locale) { DecimalFormat format = (DecimalFormat) NumberFormat.getCurrencyInstance(locale); format.setParseBigDecimal(true); format.setMaximumFractionDigits(this.fractionDigits); format.setMinimumFractionDigits(this.fractionDigits); if (this.roundingMode != null) { format.setRoundingMode(this.roundingMode); } if (this.currency != null) { format.setCurrency(this.currency); } if (this.pattern != null) { format.applyPattern(this.pattern); } return format; }
@Override public String parse(String text, Locale locale) throws ParseException { throw new ParseException(text, 0); } @Override
public MyDateEditor() { super(DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMAN), true); } }
public class Foo { // SimpleDateFormat is not thread-safe, so give one to each thread private static final ThreadLocal<SimpleDateFormat> formatter = new ThreadLocal<SimpleDateFormat>(){ @Override protected SimpleDateFormat initialValue() { return new SimpleDateFormat("yyyyMMdd HHmm"); } }; public String formatIt(Date date) { return formatter.get().format(date); } }
String string = "January 2, 2010"; DateFormat format = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH); Date date = format.parse(string); System.out.println(date); // Sat Jan 02 00:00:00 GMT 2010
private DateFormat newDateFormat() { SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.US); dateFormat.setTimeZone(GMT); return dateFormat; }
private Date getEndDate() throws ParseException { Date end = df.parse("2004/11/13"); return end; }
@Override public String parse(String text, Locale locale) throws ParseException { throw new ParseException(text, 0); } @Override