List of usage examples for java.util Date getTime
public long getTime()
From source file:Main.java
public static String calculateByDate(String date, String format, int dayOffset) { SimpleDateFormat formater = new SimpleDateFormat(); try {/* w ww .ja v a 2s . co m*/ formater.applyPattern(format); Date time = formater.parse(date); long ts = time.getTime() + dayOffset * 24 * 3600 * 1000L; Date newDate = new Date(ts); return date2String(format, newDate); } catch (ParseException e) { e.printStackTrace(); return null; } }
From source file:Main.java
public static long formatTime(String time) { SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); Date date; try {//from www. jav a 2 s.co m date = sdf.parse(time); long millis = date.getTime(); return millis; } catch (ParseException e) { e.printStackTrace(); } return 0; }
From source file:Main.java
public static Date firstTimeOfDate(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(date.getTime()); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); Date dateBegin = new Date(); dateBegin.setTime(calendar.getTimeInMillis()); return dateBegin; }
From source file:Main.java
/** * Method to check difference between actual time and saved time in xml file. * /* www . j a va 2 s . c o m*/ * @param target document * @return time difference as integer * @throws ParseException */ public static int checkDateDifference(Document target) throws ParseException { int diff = 0; NodeList parents = target.getElementsByTagName("g:options"); Element parent = (Element) parents.item(0); //g:options - only 1 element DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.ENGLISH); //date xml String time = parent.getAttribute("time"); Date oldT = format.parse(time); //date now Date newT = new Date(); //compare diff = (int) ((newT.getTime() - oldT.getTime()) / 1000); return diff; }
From source file:Main.java
/** * time1 > time2 ->return >1; time1=time2 >0;time1<time2 >-1 * @param time1/*ww w . java 2s .co m*/ * @param time2 * @return */ public static int compareTime(String time1, String time2) { try { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date1 = sdf.parse(time1); Date date2 = sdf.parse(time2); long ret = (date1.getTime() - date2.getTime()); if (ret > 0) return 1; else if (ret < 0) return -1; else return 0; } catch (Exception e) { } return 0; }
From source file:Main.java
public static String getDateStr(Date date, String format) { if (date == null) { return ""; }// w w w. j a v a2s. c o m return getDateStr(date.getTime(), format); }
From source file:Util.java
public static int getAge(Date birthDate) { if (birthDate == null) return 0; long ageMillis = System.currentTimeMillis() - birthDate.getTime(); return (int) (ageMillis / MILLIS_PER_YEAR); }
From source file:Main.java
public static String getTwoDay(String sj1, String sj2) { SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd"); long day = 0; try {/* w ww . j a v a2 s .c o m*/ Date date = myFormatter.parse(sj1); Date mydate = myFormatter.parse(sj2); day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000); } catch (Exception e) { return ""; } return day + ""; }
From source file:Util.java
public static Timestamp formatTimestamp(Timestamp datetime) { try {/*from w ww.j a v a2 s . co m*/ DateFormat formatter; String dateFormat = datetime.toString(); System.out.println("Truoc:" + datetime); formatter = new SimpleDateFormat("dd/MM/yyyy"); Date date = (Date) formatter.parse(dateFormat); java.sql.Timestamp timeStampDate = new Timestamp(date.getTime()); System.out.println("Sau:" + timeStampDate); return timeStampDate; } catch (ParseException e) { System.out.println("Exception :" + e); return null; } }
From source file:Main.java
public static boolean isCurrentTimeInBetween(String startTime, String endTime, String format) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format); try {//from ww w . j a va 2 s. c o m Date startDate = simpleDateFormat.parse(startTime); long startTimeLong = startDate.getTime(); Date endDate = simpleDateFormat.parse(endTime); long endTimeLong = endDate.getTime(); Date currDate = new Date(System.currentTimeMillis()); String currTime = simpleDateFormat.format(currDate); currDate = simpleDateFormat.parse(currTime); long currentTime = currDate.getTime(); return currentTime >= startTimeLong && currentTime < endTimeLong; } catch (ParseException e) { e.printStackTrace(); } return false; }