private static long parseIso8601(String value) { try { return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z").parse(value).getTime(); } catch (ParseException ignored) { return 0; } }
/** * Gets the millis, which is the Calendar millis value. * * @param object the Calendar to convert, must not be null * @param chrono the chronology result from getChronology, non-null * @return the millisecond value * @throws NullPointerException if the object is null * @throws ClassCastException if the object is an invalid type */ public long getInstantMillis(Object object, Chronology chrono) { Calendar calendar = (Calendar) object; return calendar.getTime().getTime(); }
public class MainClass { public static void main(String[] args) { java.util.Date utilDate = new java.util.Date(); java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); System.out.println("utilDate:" + utilDate); System.out.println("sqlDate:" + sqlDate); } }
try { SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy"); Date past = format.parse("01/10/2010"); Date now = new Date(); System.out.println(TimeUnit.MILLISECONDS.toMillis(now.getTime() - past.getTime()) + " milliseconds ago"); System.out.println(TimeUnit.MILLISECONDS.toMinutes(now.getTime() - past.getTime()) + " minutes ago"); System.out.println(TimeUnit.MILLISECONDS.toHours(now.getTime() - past.getTime()) + " hours ago"); System.out.println(TimeUnit.MILLISECONDS.toDays(now.getTime() - past.getTime()) + " days ago"); } catch (Exception j){ j.printStackTrace(); }
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()); }
/** * Returns the time after which a task should run again. */ @Override public Date nextExecutionTime(TriggerContext triggerContext) { Date lastExecution = triggerContext.lastScheduledExecutionTime(); Date lastCompletion = triggerContext.lastCompletionTime(); if (lastExecution == null || lastCompletion == null) { return new Date(System.currentTimeMillis() + this.initialDelay); } if (this.fixedRate) { return new Date(lastExecution.getTime() + this.period); } return new Date(lastCompletion.getTime() + this.period); }
public static Long toEpoch(Date dateTime, String timeZone) { // Epoch of midnight in local time zone Calendar timeOffset = Calendar.getInstance(TimeZone.getTimeZone(timeZone)); timeOffset.set(Calendar.MILLISECOND, 0); timeOffset.set(Calendar.SECOND, 0); timeOffset.set(Calendar.MINUTE, 0); timeOffset.set(Calendar.HOUR_OF_DAY, 0); long midnightOffSet = timeOffset.getTime().getTime(); long localTimestamp = dateTime.getTime(); return timeOffset == null ? null : midnightOffSet + localTimestamp; }
public long getTimestamp() throws IOException { long deltaServerTime = ((BinanceExchange) exchange).deltaServerTime(); Date systemTime = new Date(System.currentTimeMillis()); Date serverTime = new Date(systemTime.getTime() + deltaServerTime); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS"); LOG.trace( "getTimestamp: {} + {} => {}", df.format(systemTime), deltaServerTime, df.format(serverTime)); return serverTime.getTime(); }
String time1 = "16:00:00"; String time2 = "19:00:00"; SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss"); Date date1 = format.parse(time1); Date date2 = format.parse(time2); long difference = date2.getTime() - date1.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); } } }
@Override public Date fromString(String string) { try { return new Timestamp( new SimpleDateFormat( TIMESTAMP_FORMAT ).parse( string ).getTime() ); } catch ( ParseException pe) { throw new HibernateException( "could not parse timestamp string" + string, pe ); } }
@Test public void testTimestamp() throws Exception { AccumuloRowSerializer serializer = serializerClass.getConstructor().newInstance(); Type type = TIMESTAMP; Timestamp expected = new Timestamp(new java.util.Date().getTime()); byte[] data = serializer.encode(type, expected); Timestamp actual = new Timestamp(serializer.decode(type, data)); assertEquals(actual, expected); deserializeData(serializer, data); actual = serializer.getTimestamp(COLUMN_NAME); assertEquals(actual, expected); }
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); Date date = dateFormat.parse("23/09/2007"); long time = date.getTime(); new Timestamp(time);
public Person createModesty() { Address benhome = new Address("NN Island", "Blue Cave", "ta", new Zipcode("82742")); Address benwork = new Address("44 Planetary St.", "Neptune", "Milkiway", new Zipcode("12345")); Calendar benCal = Calendar.getInstance(); benCal.set(1986, Calendar.AUGUST, 8, 8, 11); Person ben = new Person("Modesty", "Blase", benCal.getTime(), benhome, benwork); benCal = Calendar.getInstance(); benCal.set(1995, Calendar.MAY, 21, 8, 11); ben.setFirstBaseBallGame(new Timestamp(benCal.getTime().getTime())); ben.getHobbies().add("sneak"); ben.getHobbies().add("kill"); ben.getHobbies().add("fight"); return ben; }
@Test public void testSetParameterValueWithCalendarAndUnknownType() throws SQLException { java.util.Calendar cal = new GregorianCalendar(); StatementCreatorUtils.setParameterValue(preparedStatement, 1, SqlTypeValue.TYPE_UNKNOWN, null, cal); verify(preparedStatement).setTimestamp(1, new java.sql.Timestamp(cal.getTime().getTime()), cal); }
public long getDateHeader(String name) { String headerValue = getHeader(name); if (headerValue == null) { return -1; } try { return newDateFormat().parse(getHeader(name)).getTime(); } catch (ParseException ex) { throw new IllegalArgumentException( "Value for header '" + name + "' is not a valid Date: " + headerValue); } }
SimpleDateFormat myFormat = new SimpleDateFormat("dd MM yyyy"); String inputString1 = "23 01 1997"; String inputString2 = "27 04 1997"; try { Date date1 = myFormat.parse(inputString1); Date date2 = myFormat.parse(inputString2); long diff = date2.getTime() - date1.getTime(); System.out.println ("Days: " + TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS)); } catch (ParseException e) { e.printStackTrace(); }
@Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("开始执行过滤器"); Long start = new Date().getTime(); filterChain.doFilter(servletRequest, servletResponse); System.out.println("【过滤器】耗时 " + (new Date().getTime() - start)); System.out.println("结束执行过滤器"); }
java.util.Calendar cal = Calendar.getInstance(); java.util.Date utilDate = new java.util.Date(); // your util date cal.setTime(utilDate); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); java.sql.Date sqlDate = new java.sql.Date(cal.getTime().getTime()); // your sql date System.out.println("utilDate:" + utilDate); System.out.println("sqlDate:" + sqlDate);