Refine search
Collections.sort(datestring, new Comparator<String>() { DateFormat f = new SimpleDateFormat("dd/MM/yyyy '@'hh:mm a"); @Override public int compare(String o1, String o2) { try { return f.parse(o1).compareTo(f.parse(o2)); } catch (ParseException e) { throw new IllegalArgumentException(e); } } });
public static boolean compareDate(String date1, String date2, String df) { SimpleDateFormat sdf = new SimpleDateFormat(df); try { Date d1 = sdf.parse(date1); Date d2 = sdf.parse(date2); return d1.compareTo(d2) > 0; } catch (ParseException e) { return false; } }
import java.util.Date; import java.util.Calendar; import java.text.SimpleDateFormat; import static java.lang.System.out; import static java.util.Calendar.MONTH; public class BeforeTest { public static void main( String [] args ) throws java.text.ParseException { SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); out.println( isBeforeMonths( -1 , sdf.parse("14/12/2009"))); out.println( isBeforeMonths( -1 , new Date() )); out.println( isBeforeMonths( -1 , sdf.parse("24/12/2009"))); } private static boolean isBeforeMonths( int months, Date aDate ) { Calendar calendar = Calendar.getInstance(); calendar.add( MONTH , months ); return aDate.compareTo( calendar.getTime() ) < 0; } }
@Override public void postSend(Message<?> message, MessageChannel channel, boolean sent) { Object payload = message.getPayload(); assertThat(payload, instanceOf(String.class)); Date date = null; try { date = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.DEFAULT).parse((String) payload); } catch (ParseException e) { fail("fail to parse date"); } assertThat(new Date().compareTo(date), greaterThanOrEqualTo(0)); stopLatch.countDown(); }
max = DateFormat.getDateTimeInstance().parse(constraints[i].getValue()); } catch (ParseException e) { throw new SAXException(e); max = DateFormat.getDateTimeInstance().parse(constraints[i].getValue()); } catch (ParseException e) { throw new SAXException(e); if (dval.compareTo(max) > 0) { throw new SAXException("Too large a value"); max = DateFormat.getDateTimeInstance().parse(constraints[i].getValue()); } catch (ParseException e) { throw new SAXException(e); if (dval.compareTo(max) < 0) { throw new SAXException("Too large a value");
public static void removeToken(String key) { if (Constant.cacheEnabled) { SimpleCache.instance().remove(OAUTH2_TOKEN_DEF_KEY, key); } else { tokens.remove(key); for (Map.Entry<String, Token> tokenEntry : tokens.entrySet()) { if (tokenEntry.getValue().getExpiredAt().compareTo(new Date()) < 0) { tokens.remove(tokenEntry.getKey()); refreshTokens.values().remove(tokenEntry.getKey()); } } } }
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d'T'HH:mm:ss"); Date date = sdf.parse(dateStr); if (date.compareTo(new Date()) > 0) { // log an error }
String pattern = "HH:mm"; SimpleDateFormat sdf = new SimpleDateFormat(pattern); try { Date date1 = sdf.parse("19:28"); Date date2 = sdf.parse("21:13"); // Outputs -1 as date1 is before date2 System.out.println(date1.compareTo(date2)); // Outputs 1 as date1 is after date1 System.out.println(date2.compareTo(date1)); date2 = sdf.parse("19:28"); // Outputs 0 as the dates are now equal System.out.println(date1.compareTo(date2)); } catch (ParseException e){ // Exception handling goes here }
private Map<Long, Long> getIntervals(long lowWatermarkValue, long highWatermarkValue, int partitionInterval) throws ParseException { Map<Long, Long> intervals = new HashMap<Long, Long>(); if (lowWatermarkValue > highWatermarkValue || partitionInterval <= 0) return intervals; if (lowWatermarkValue == highWatermarkValue) { return ImmutableMap.of(lowWatermarkValue, highWatermarkValue); } final SimpleDateFormat inputFormat = new SimpleDateFormat(this.watermarkFormat); Date startTime = inputFormat.parse(String.valueOf(lowWatermarkValue)); Date endTime = inputFormat.parse(String.valueOf(highWatermarkValue)); Calendar cal = Calendar.getInstance(); while (startTime.compareTo(endTime) < 0) { cal.setTime(startTime); cal.add(Calendar.HOUR, partitionInterval); Date nextTime = cal.getTime(); if (nextTime.compareTo(endTime) > 0) { nextTime = endTime; } intervals.put(Long.parseLong(inputFormat.format(startTime)), Long.parseLong(inputFormat.format(nextTime))); startTime = nextTime; } return intervals; } }
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date timeCompare = sdf.parse(time); Date currentTime = new Date(); int comareResult = timeCompare.compareTo(currentTime); if (comareResult = 0){ ... }
try{ SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); String str1 = "12/10/2013"; Date date1 = formatter.parse(str1); String str2 = "13/10/2013"; Date date2 = formatter.parse(str2); if (date1.compareTo(date2)<0) { System.out.println("date2 is Greater than my date1"); } }catch (ParseException e1){ e1.printStackTrace(); }
private int tryCompareDate(final Object thisValue, final Object otherValue) { try { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS"); Date otherDate = simpleDateFormat.parse((String) otherValue); return ((Date) thisValue).compareTo(otherDate); } catch (Throwable t) { throw new ExpressionRuntimeException("Compare date error", t); } }
public static void removeCode(String key) { if (Constant.cacheEnabled) { SimpleCache.instance().remove(OAUTH2_CODE_DEF_KEY, key); } else { codes.remove(key); for (Map.Entry<String, Code> codeEntry : codes.entrySet()) { if (codeEntry.getValue().getExpiredAt().compareTo(new Date()) < 0) { codes.remove(codeEntry.getKey()); } } } }
String dob="02/27/2013"; Date today = new Date(); try { Date dobDate = new SimpleDateFormat("MM/dd/yyyy").parse(dob); if (dobDate.compareTo(today) <= 0) { //before or equals today System.out.println("before"); } } catch (ParseException e) { //handle exception }
class StringDateComparator implements Comparator<String> { SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); public int compare(String lhs, String rhs) { return dateFormat.parse(lhs).compareTo(dateFormat.parse(rhs)); } } Collections.sort(arrayList, new StringDateComparator());
private List<Cookie> parseCookies(String url, String cookieHeader) { String[] fields = cookieHeader.split(";", 0); List<String> parsedFields = new ArrayList<>(); Date expiration = null; boolean isSecure = false; for (String field : fields) { field = field.trim(); if (field.startsWith(EXPIRATION_FIELD_NAME)) { expiration = getExpiration(field); } else if (field.toUpperCase().equals(SECURE_ATTR_NAME)) { isSecure = true; } else { parsedFields.add(field); } } String hostname = getCookieHost(url); List<Cookie> cookies = new ArrayList<>(); for (String cookie : parsedFields) { if (expiration == null || expiration.compareTo(new Date()) >= 0) { cookies.add(new Cookie(hostname, isSecure, cookie, expiration)); } } return cookies; }