canonical example by Tabnine
public boolean isDateExpired(String input, Date expiration) throws ParseException { DateFormat dateFormat = new SimpleDateFormat ("dd/MM/yyyy"); Date date = dateFormat.parse(input); return date.after(expiration); }
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()); }
private static Date getDayBeginTime(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.HOUR, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); return new Date(calendar.getTime().getTime()); }
public static Date getDateOffset(int offset) { Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.add(calendar.DATE, offset); return getDayBeginTime(calendar.getTime()); }
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); Calendar c = Calendar.getInstance(); c.setTime(new Date()); // Now use today date. c.add(Calendar.DATE, 5); // Adding 5 days String output = sdf.format(c.getTime()); System.out.println(output);
try { final TimeZone denverZone = TimeZone.getTimeZone("America/Denver"); TimeZone.setDefault(denverZone); final DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS XXX"); format.setTimeZone(denverZone); final Date oct31_01MDT = new Date(1099206000000L); final Date oct31MDT = new Date(oct31_01MDT.getTime() - 3600000L); // - 1 hour final Date oct31_01_02MDT = new Date(oct31_01MDT.getTime() + 120000L); // + 2 minutes final Date oct31_01_02_03MDT = new Date(oct31_01_02MDT.getTime() + 3000L); // + 3 seconds final Date oct31_01_02_03_04MDT = new Date(oct31_01_02_03MDT.getTime() + 4L); // + 4 milliseconds assertEquals("Check 00:00:00.000", "2004-10-31 00:00:00.000 -06:00", format.format(oct31MDT)); assertEquals("Check 01:00:00.000", "2004-10-31 01:00:00.000 -06:00", format.format(oct31_01MDT)); assertEquals("Check 01:02:00.000", "2004-10-31 01:02:00.000 -06:00", format.format(oct31_01_02MDT)); assertEquals("Check 01:02:03.000", "2004-10-31 01:02:03.000 -06:00", format.format(oct31_01_02_03MDT)); final Calendar gval = Calendar.getInstance(); gval.setTime(new Date(oct31_01MDT.getTime())); gval.set(Calendar.MINUTE, gval.get(Calendar.MINUTE)); // set minutes to the same value assertEquals("Demonstrate Problem", gval.getTime().getTime(), oct31_01MDT.getTime() + 3600000L); } finally { TimeZone.setDefault(defaultZone);
Calendar c = Calendar.getInstance(); c.setTime(new Date(yourmilliseconds)); c.set(Calendar.MILLISECOND, 0); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm.ss.SSS'Z'"); sdf.setTimeZone(TimeZone.getTimeZone("GMT")); System.out.println(sdf.format(c.getTime()));
UTC = TimeZone.getTimeZone("UTC"); TimeZone.setDefault(UTC); final Calendar c = new GregorianCalendar(UTC); c.set(1, 0, 1, 0, 0, 0); c.set(Calendar.MILLISECOND, 0); BEGINNING_OF_TIME = c.getTime(); c.setTime(new Date(Long.MAX_VALUE)); END_OF_TIME = c.getTime(); final SimpleDateFormat sdf = new SimpleDateFormat(ISO_8601_24H_FULL_FORMAT); sdf.setTimeZone(UTC); System.out.println("sdf.format(BEGINNING_OF_TIME) = " + sdf.format(BEGINNING_OF_TIME)); System.out.println("sdf.format(END_OF_TIME) = " + sdf.format(END_OF_TIME)); System.out.println("sdf.format(new Date()) = " + sdf.format(new Date())); System.out.println("sdf.parse(\"2015-04-28T14:23:38.521Z\") = " + sdf.parse("2015-04-28T14:23:38.521Z")); System.out.println("sdf.parse(\"0001-01-01T00:00:00.000Z\") = " + sdf.parse("0001-01-01T00:00:00.000Z"));
public String getCurTime() { String fromTimeZone = "GMT+8"; SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(); format.setTimeZone(TimeZone.getTimeZone(fromTimeZone)); String chinaDate = format.format(date); return chinaDate; }
public static List<String> getOldWeekDays() { final Calendar c = Calendar.getInstance(); String[] months = new String[8]; for (int i = 0; i < 8; i++) { months[i] = new SimpleDateFormat("MM.dd").format(new Date(c .getTimeInMillis())); c.add(Calendar.DAY_OF_MONTH, -1); } return Arrays.asList(months); }
java.util.Date utilDate = new java.util.Date(); Calendar cal = Calendar.getInstance(); cal.setTime(utilDate); cal.set(Calendar.MILLISECOND, 0); System.out.println(new java.sql.Timestamp(utilDate.getTime())); System.out.println(new java.sql.Timestamp(cal.getTimeInMillis()));
/** * Sets the Date and Cache headers for the HTTP Response * * @param response HTTP response * @param fileToCache file to extract content type */ private static void setDateAndCacheHeaders(HttpResponse response, File fileToCache) { SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US); dateFormatter.setTimeZone(TimeZone.getTimeZone(HTTP_DATE_GMT_TIMEZONE)); // Date header Calendar time = new GregorianCalendar(); response.headers().set(DATE, dateFormatter.format(time.getTime())); // Add cache headers time.add(Calendar.SECOND, HTTP_CACHE_SECONDS); response.headers().set(EXPIRES, dateFormatter.format(time.getTime())); response.headers().set(CACHE_CONTROL, "private, max-age=" + HTTP_CACHE_SECONDS); response.headers().set( LAST_MODIFIED, dateFormatter.format(new Date(fileToCache.lastModified()))); }
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss"); String currentDateandTime = sdf.format(new Date());
if (!Date.now) { Date.now = function() { return new Date().getTime(); } }
public void doRun() { while(new Date().getTime() >= cal.getTimeInMillis()) { LOGGER.log(Level.FINE, "cron checking {0}", cal.getTime()); try { checkTriggers(cal); } catch (Throwable e) { LOGGER.log(Level.WARNING,"Cron thread throw an exception",e); // SafeTimerTask.run would also catch this, but be sure to increment cal too. } cal.add(Calendar.MINUTE,1); } } }
private String getTimeoutMessage(boolean scan) { long nowTimestamp = System.currentTimeMillis(); return (sent > 0 ? "Waiting server-side response timeout" : "Sending request timeout in client-side") + (scan ? " by scan timer" : "") + ". start time: " + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date(start))) + ", end time: " + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date())) + "," + (sent > 0 ? " client elapsed: " + (sent - start) + " ms, server elapsed: " + (nowTimestamp - sent) : " elapsed: " + (nowTimestamp - start)) + " ms, timeout: " + timeout + " ms, request: " + request + ", channel: " + channel.getLocalAddress() + " -> " + channel.getRemoteAddress(); } }
@Test public void shouldHandleLastModifiedWithHttp304() throws Exception { long currentTime = new Date().getTime(); long oneMinuteAgo = currentTime - (1000 * 60); ZonedDateTime dateTime = ofEpochMilli(currentTime).atZone(GMT); servletRequest.addHeader(HttpHeaders.IF_MODIFIED_SINCE, RFC_1123_DATE_TIME.format(dateTime)); ResponseEntity<String> returnValue = ResponseEntity.ok().lastModified(oneMinuteAgo).body("body"); initStringMessageConversion(TEXT_PLAIN); processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest); assertConditionalResponse(HttpStatus.NOT_MODIFIED, null, null, oneMinuteAgo); }
@Test public void testCalendar() { Date d = new Date(0); Calendar cal = Calendar.getInstance(); cal.setTime(d); assertThat(jdkTypeToJson(cal), containsString(new SimpleDateFormat("yyyy-MM-dd").format(d))); }