// 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 newstring = new SimpleDateFormat("yyyy-MM-dd").format(date); System.out.println(newstring); // 2011-01-18
Date now = new Date(); // java.util.Date, NOT java.sql.Date or java.sql.Timestamp! String format1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.ENGLISH).format(now); String format2 = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH).format(now); String format3 = new SimpleDateFormat("yyyyMMddHHmmss", Locale.ENGLISH).format(now); System.out.println(format1); System.out.println(format2); System.out.println(format3);
private static String clientDateString() { TimeZone tz = TimeZone.getTimeZone("UTC"); DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"); df.setTimeZone(tz); // strip timezone return df.format(new Date()); }
Calendar cal = new GregorianCalendar(); SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); dateFormat.setTimeZone(cal.getTimeZone()); System.out.println(dateFormat.format(cal.getTime()));
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())); }
String dateStr = "04/05/2010"; SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yyyy"); Date dateObj = curFormater.parse(dateStr); SimpleDateFormat postFormater = new SimpleDateFormat("MMMM dd, yyyy"); String newDateStr = postFormater.format(dateObj);
@Test public void getTimeFormat_returnsATimeFormat() { Calendar cal = Calendar.getInstance(); cal.clear(); cal.set(Calendar.HOUR, 7); cal.set(Calendar.MINUTE, 48); cal.set(Calendar.SECOND, 3); Date date = cal.getTime(); assertThat(getTimeFormat(context).format(date)).isEqualTo("07:48:03"); }
public void write(JSONSerializer serializer, Object object, BeanContext context) throws IOException { SerializeWriter out = serializer.out; String format = context.getFormat(); Calendar calendar = (Calendar) object; if (format.equals("unixtime")) { long seconds = calendar.getTimeInMillis() / 1000L; out.writeInt((int) seconds); return; } DateFormat dateFormat = new SimpleDateFormat(format); if (dateFormat == null) { dateFormat = new SimpleDateFormat(JSON.DEFFAULT_DATE_FORMAT, serializer.locale); dateFormat.setTimeZone(serializer.timeZone); } String text = dateFormat.format(calendar.getTime()); out.writeString(text); }
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); } }
private void updateValidationsForNextRun(Collection<FormValidation> validations, CronTabList ctl) { try { Calendar prev = ctl.previous(); Calendar next = ctl.next(); if (prev != null && next != null) { DateFormat fmt = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL); validations.add(FormValidation.ok(Messages.TimerTrigger_would_last_have_run_at_would_next_run_at(fmt.format(prev.getTime()), fmt.format(next.getTime())))); } else { validations.add(FormValidation.warning(Messages.TimerTrigger_no_schedules_so_will_never_run())); } } catch (RareOrImpossibleDateException ex) { validations.add(FormValidation.warning(Messages.TimerTrigger_the_specified_cron_tab_is_rare_or_impossible())); } } }
// Format for input SimpleDateFormat dateParser = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); // Parsing the date Date date = dateParser.parse(dateTime); // Format for output SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy"); // Printing the date System.out.println(dateFormatter.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
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss"); String currentDateandTime = sdf.format(new Date());
String input = "Thu Jun 18 20:56:02 EDT 2009"; SimpleDateFormat parser = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy"); Date date = parser.parse(input); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); String formattedDate = formatter.format(date); ...
@Test public void getDateFormat_returnsADateFormat_December() { Calendar cal = Calendar.getInstance(); cal.clear(); cal.set(Calendar.DATE, 31); cal.set(Calendar.MONTH, Calendar.DECEMBER); cal.set(Calendar.YEAR, 1970); Date date = cal.getTime(); assertThat(getDateFormat(context).format(date)).isEqualTo("Dec-31-1970"); }
/** * Returns the build time stamp in the body. */ public void doBuildTimestamp( StaplerRequest req, StaplerResponse rsp, @QueryParameter String format) throws IOException { rsp.setContentType("text/plain"); rsp.setCharacterEncoding("US-ASCII"); rsp.setStatus(HttpServletResponse.SC_OK); DateFormat df = format==null ? DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT, Locale.ENGLISH) : new SimpleDateFormat(format,req.getLocale()); rsp.getWriter().print(df.format(getTime())); }