Refine search
public static String toTimeStr(Date time) { int hour = time.getHours(); int min = time.getMinutes(); StringBuilder sb = new StringBuilder(); if (hour < 10) { sb.append(0).append(hour); } else { sb.append(hour); } sb.append(":"); if (min < 10) { sb.append(0).append(min); } else { sb.append(min); } return sb.toString(); }
/** * Constructs a TimeOfDay from a <code>java.util.Date</code> * using exactly the same field values avoiding any time zone effects. * <p> * Each field is queried from the Date and assigned to the TimeOfDay. * This is useful to ensure that the field values are the same in the * created TimeOfDay no matter what the time zone is. For example, if * the Calendar states that the time is 04:29, then the created TimeOfDay * will always have the time 04:29 irrespective of time zone issues. * <p> * This factory method always creates a TimeOfDay with ISO chronology. * * @param date the Date to extract fields from * @return the created TimeOfDay * @throws IllegalArgumentException if the calendar is null * @throws IllegalArgumentException if the date is invalid for the ISO chronology * @since 1.2 */ public static TimeOfDay fromDateFields(Date date) { if (date == null) { throw new IllegalArgumentException("The date must not be null"); } return new TimeOfDay( date.getHours(), date.getMinutes(), date.getSeconds(), (((int) (date.getTime() % 1000)) + 1000) % 1000 ); }
You can use below code to run TimerTask every hours HH:00:- Timer timer = new Timer(); Calendar cd = Calendar.getInstance(); Date dt = cd.getTime(); long mmss = dt.getMinutes() * 60 + dt.getSeconds(); long remTime = 60 * 60 * 60 - mmss; timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { // TODO Auto-generated method stub } }, remTime * 1000, 60*60 * 60 * 1000);
date.getHours(), date.getMinutes(), date.getSeconds(), (((int) (date.getTime() % 1000)) + 1000) % 1000 );
final Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(System.currentTimeMillis()); Date date = cal.getTime(); mHour = date.getHours(); mMinute = date.getMinutes();
@Deprecated @JTranscMethodBody(target = "js", value = "this._date.setHours(p0);") public void setHours(int hours) { this.setTimestamp(JTranscTime.make(getFullYear(), getMonth(), getDate(), hours, getMinutes(), getSeconds(), getMilliseconds())); }
/** * Constructs a TimeOfDay from a <code>java.util.Date</code> * using exactly the same field values avoiding any time zone effects. * <p> * Each field is queried from the Date and assigned to the TimeOfDay. * This is useful to ensure that the field values are the same in the * created TimeOfDay no matter what the time zone is. For example, if * the Calendar states that the time is 04:29, then the created TimeOfDay * will always have the time 04:29 irrespective of time zone issues. * <p> * This factory method always creates a TimeOfDay with ISO chronology. * * @param date the Date to extract fields from * @return the created TimeOfDay * @throws IllegalArgumentException if the calendar is null * @throws IllegalArgumentException if the date is invalid for the ISO chronology * @since 1.2 */ public static TimeOfDay fromDateFields(Date date) { if (date == null) { throw new IllegalArgumentException("The date must not be null"); } return new TimeOfDay( date.getHours(), date.getMinutes(), date.getSeconds(), (((int) (date.getTime() % 1000)) + 1000) % 1000 ); }
/** * @throws HL7Exception * @see HL7Util#parseHL7Time(String) */ @Test @SuppressWarnings("deprecation") public void parseHL7Time_shouldHandle0615() throws HL7Exception { // set tz to be a __non DST__ timezone so this junit test works everywhere and always TimeZone originalTimeZone = TimeZone.getDefault(); TimeZone.setDefault(TimeZone.getTimeZone("EAT")); Date parsedDate = HL7Util.parseHL7Time("0615"); Assert.assertEquals(6, parsedDate.getHours()); Assert.assertEquals(15, parsedDate.getMinutes()); // reset the timezone TimeZone.setDefault(originalTimeZone); }
date.getHours(), date.getMinutes(), date.getSeconds(), (((int) (date.getTime() % 1000)) + 1000) % 1000 );
public static int ofMinutes(Date date){ int h = date.getHours() * 60; int m = date.getMinutes(); return h+m; }
/** * Constructs a TimeOfDay from a <code>java.util.Date</code> * using exactly the same field values avoiding any time zone effects. * <p> * Each field is queried from the Date and assigned to the TimeOfDay. * This is useful to ensure that the field values are the same in the * created TimeOfDay no matter what the time zone is. For example, if * the Calendar states that the time is 04:29, then the created TimeOfDay * will always have the time 04:29 irrespective of time zone issues. * <p> * This factory method always creates a TimeOfDay with ISO chronology. * * @param date the Date to extract fields from * @return the created TimeOfDay * @throws IllegalArgumentException if the calendar is null * @throws IllegalArgumentException if the date is invalid for the ISO chronology * @since 1.2 */ public static TimeOfDay fromDateFields(Date date) { if (date == null) { throw new IllegalArgumentException("The date must not be null"); } return new TimeOfDay( date.getHours(), date.getMinutes(), date.getSeconds(), (((int) (date.getTime() % 1000)) + 1000) % 1000 ); }
String time = "13:10"; DateFormat sdf = new SimpleDateFormat("HH:mm"); // or "hh:mm" for 12 hour format Date date = sdf.parse(time); date.getHours(); // int date.getMinutes(); // int
date.getHours(), date.getMinutes(), date.getSeconds(), (((int) (date.getTime() % 1000)) + 1000) % 1000 );
try { SimpleDateFormat format = new SimpleDateFormat("H:m"); Date date = format.parse("15:34"); int hours = date.getHours(); int min = date.getMinutes(); } catch (ParseException e) { // parsing failed }
date.getMonth() + 1, date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), (((int) (date.getTime() % 1000)) + 1000) % 1000 );
final Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(System.currentTimeMillis()); Date date = cal.getTime(); mHour = date.getHours(); mMinute = date.getMinutes();
date.getMonth() + 1, date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), (((int) (date.getTime() % 1000)) + 1000) % 1000 );
SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd hh:mm"); java.util.Date date = null; try { date = form.parse(string); } catch (ParseException e) { } SimpleDateFormat postFormater = new SimpleDateFormat("dd-MM-yyyy"); String newDateStr = "at"+date.getHours()+":"+date.getMinutes()+"on"+postFormater.format(date);
@SuppressWarnings("deprecation") public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException { if (object == null) { serializer.out.writeNull(); return; } Date date = (Date) object; JSONObject json = new JSONObject(); json.put("date", date.getDate()); json.put("day", date.getDay()); json.put("hours", date.getHours()); json.put("minutes", date.getMinutes()); json.put("month", date.getMonth()); json.put("seconds", date.getSeconds()); json.put("time", date.getTime()); json.put("timezoneOffset", date.getTimezoneOffset()); json.put("year", date.getYear()); serializer.write(json); } }
DateFormat dateFormat=new SimpleDateFormat("HH:mm:ss"); dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); // <-- Add this. Date hora=dateFormat.parse("00:00:01"); System.out.println(hora.getHours()+" "+hora.getMinutes()); System.out.println("Date "+hora); System.out.println("Seconds "+TimeUnit.MILLISECONDS.toSeconds(hora.getTime()));