List of usage examples for java.util Calendar SECOND
int SECOND
To view the source code for java.util Calendar SECOND.
Click Source Link
get
and set
indicating the second within the minute. From source file:Main.java
public static Date setDateTime(Date originalDate, int hour, int minute, int second) { Calendar cal = new GregorianCalendar(); cal.setTime(originalDate);/*w w w . j a va 2 s .co m*/ cal.set(Calendar.HOUR, hour); cal.set(Calendar.MINUTE, minute); cal.set(Calendar.SECOND, second); cal.set(Calendar.MILLISECOND, 0); return cal.getTime(); }
From source file:Main.java
/** * Packs java time value into an MS-DOS time value. * @param time the time value/*from w w w.java 2 s. c o m*/ * @return the MS-DOS packed time */ public static int packTime(long time) { Calendar c = Calendar.getInstance(); c.setTime(new Date(time)); int seconds = c.get(Calendar.SECOND); int minutes = c.get(Calendar.MINUTE); int hours = c.get(Calendar.HOUR_OF_DAY); /* * Here is how MS-DOS packs a time value: * 0-4: seconds (divided by 2 because we only have 5 bits = 32 different numbers) * 5-10: minutes (6 bits = 64 possible values) * 11-15: hours (5 bits = 32 possible values) * * source: https://msdn.microsoft.com/en-us/library/windows/desktop/ms724247(v=vs.85).aspx */ return (hours << 11) | (minutes << 5) | (seconds / 2); }
From source file:Util.java
public static byte[] packDate(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date);//w w w . ja v a 2s.com // Byte bits: 00000000 11111111 22222222 33333333 44444444 // Contents : 00YYYYYY YYYYYYMM MMDDDDDH HHHHMMMM MMSSSSSS byte[] bytes = new byte[5]; int s = c.get(Calendar.SECOND); int m = c.get(Calendar.MINUTE); int h = c.get(Calendar.HOUR_OF_DAY); int d = c.get(Calendar.DATE); int mm = c.get(Calendar.MONTH) + 1; int y = c.get(Calendar.YEAR); bytes[4] = (byte) ((m << 6) | s); bytes[3] = (byte) ((m >> 2) | (h << 4)); bytes[2] = (byte) ((h >> 4) | (d << 1) | (mm << 6)); bytes[1] = (byte) ((mm >> 2) | (y << 2)); bytes[0] = (byte) (y >> 6); return bytes; }
From source file:Main.java
/** * Decode a 16-bit encoded DOS date/time into a java date/time. * /* ww w. j a v a 2s . c om*/ * @param dosDate * @param dosTime * @return long */ public static long decodeDateTime(int dosDate, int dosTime) { final Calendar cal = Calendar.getInstance(); cal.set(Calendar.MILLISECOND, 0); cal.set(Calendar.SECOND, (dosTime & 0x1f) * 2); cal.set(Calendar.MINUTE, (dosTime >> 5) & 0x3f); cal.set(Calendar.HOUR_OF_DAY, dosTime >> 11); cal.set(Calendar.DATE, dosDate & 0x1f); cal.set(Calendar.MONTH, ((dosDate >> 5) & 0x0f) - 1); cal.set(Calendar.YEAR, 1980 + (dosDate >> 9)); return cal.getTimeInMillis(); }
From source file:Main.java
public static String calcUntil(Calendar untilDate, String granularity) { String pattern = ""; if (granularity.equals("YYYY-MM-DDThh:mm:ssZ")) { untilDate.add(Calendar.SECOND, -1); pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'"; } else {// ww w . j a v a 2s . co m untilDate.add(Calendar.DATE, -1); pattern = "yyyy-MM-dd"; } SimpleDateFormat sdf = new SimpleDateFormat(pattern); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); return sdf.format(untilDate.getTime()); }
From source file:Main.java
/** * Encode a java date/time into a 16-bit encoded DOS time * // ww w . j av a 2 s . com * @param javaDateTime * @return long */ public static int encodeTime(long javaDateTime) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(javaDateTime); return 2048 * cal.get(Calendar.HOUR_OF_DAY) + 32 * cal.get(Calendar.MINUTE) + cal.get(Calendar.SECOND) / 2; }
From source file:Main.java
/** * Converts input time from Java to DOS format * @param time/*from w w w .j a va 2s . c om*/ * @return time in DOS format */ public static long javaToDosTime(long time) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(time); int year = cal.get(Calendar.YEAR); if (year < 1980) { return (1 << 21) | (1 << 16); } return (year - 1980) << 25 | (cal.get(Calendar.MONTH) + 1) << 21 | cal.get(Calendar.DATE) << 16 | cal.get(Calendar.HOUR_OF_DAY) << 11 | cal.get(Calendar.MINUTE) << 5 | cal.get(Calendar.SECOND) >> 1; }
From source file:edu.zipcloud.core.util.DateUtil.java
public static Date getEndOfDay(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date);//from w w w. j ava 2 s . c o m calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); calendar.set(Calendar.MILLISECOND, 999); return calendar.getTime(); }
From source file:Main.java
/** * Creates a calendar object with the next absolute time determined by hour and minute * @param hourOfDay/*from www .j ava2 s . com*/ * @param minute * @return */ public static Calendar getNextAlarmTimeAbsolute(int hourOfDay, int minute) { Calendar cal = Calendar.getInstance(); Calendar calNow = (Calendar) cal.clone(); cal.set(Calendar.HOUR_OF_DAY, hourOfDay); cal.set(Calendar.MINUTE, minute); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); if (cal.compareTo(calNow) <= 0) { //Today's time passed, count to tomorrow cal.add(Calendar.DATE, 1); } return cal; }
From source file:Main.java
public static int[] getTimeFields(long time) { Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(time);/*from www . jav a 2 s .c o m*/ int[] timeFields = new int[6]; timeFields[0] = calendar.get(Calendar.YEAR); timeFields[1] = calendar.get(Calendar.MONTH); timeFields[2] = calendar.get(Calendar.DAY_OF_MONTH); timeFields[3] = calendar.get(Calendar.HOUR_OF_DAY); timeFields[4] = calendar.get(Calendar.MINUTE); timeFields[5] = calendar.get(Calendar.SECOND); return timeFields; }