void dateTest() throws RuntimeException { Locale locTH = new Locale("th", "TH", "TH"); TimeZone tz = TimeZone.getTimeZone("PST"); Calendar calGregorian = Calendar.getInstance(tz, Locale.US); calGregorian.clear(); calGregorian.set(2002, 4, 1, 8, 30); final Date date = calGregorian.getTime(); Calendar cal = Calendar.getInstance(tz, locTH); cal.clear(); cal.setTime(date); final String strExpected = "\u0E27\u0E31\u0E19\u0E1E\u0E38\u0E18\u0E17\u0E35\u0E48\u0020\u0E51\u0020\u0E1E\u0E24\u0E29\u0E20\u0E32\u0E04\u0E21\u0020\u0E1E\u002E\u0E28\u002E\u0020\u0E52\u0E55\u0E54\u0E55\u002C\u0020\u0E58\u0020\u0E19\u0E32\u0E2C\u0E34\u0E01\u0E32\u0020\u0E53\u0E50\u0020\u0E19\u0E32\u0E17\u0E35\u0020\u0E50\u0E50\u0020\u0E27\u0E34\u0E19\u0E32\u0E17\u0E35"; Date value = cal.getTime(); // th_TH_TH test DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locTH); df.setTimeZone(tz); String str = df.format(value); if (!strExpected.equals(str)) { throw new RuntimeException(); } }
private String getDate(String timeStampStr, String format) { DateFormat formatter = new SimpleDateFormat(format); long timeStamp = Long.parseLong(timeStampStr); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(timeStamp); return formatter.format(calendar.getTime()); }
private void doTest(String candidate, int expectedHours, int expectedMins) throws ParseException { DateFormat df = new SimpleDateFormat("HH:mm"); Date result = df.parse(candidate); Calendar cal = Calendar.getInstance(); cal.setTime(result); assertEquals(expectedHours, cal.get(Calendar.HOUR_OF_DAY)); assertEquals(expectedMins, cal.get(Calendar.MINUTE)); }
private static long toMillis( int year, int month, int day, int hour, int min, int sec, int millis, TimeZone timeZone) { checkMonth(month); Calendar c = new GregorianCalendar(timeZone); c.set(year, month - 1, day, hour, min, sec); c.set(Calendar.MILLISECOND, millis); return c.getTime().getTime(); }
/** * Gets a temporary Calendar set to the specified time zone. The same Calendar is returned on * subsequent calls. */ protected Calendar getCalendar(TimeZone zone) { if (tempCal == null) { tempCal = Calendar.getInstance(zone); } else { tempCal.setTimeZone(zone); } return tempCal; }
private static Date sampleDate() { Calendar calendar = Calendar.getInstance(); // Thursday 2008-04-24 7:10:45pm // Chose a Thursday because 2008 starts on a Tuesday - it makes weeks // interesting. calendar.set(2008, 3 /* 0-based! */, 24, 19, 10, 45); return calendar.getTime(); }
/** Initializes contained components. */ private void initComponents() { final SimpleDateFormat format = new SimpleDateFormat("mm:ss"); final Calendar c = Calendar.getInstance(); final JLabel counter = new JLabel(); counter.setForeground(Color.red); counter.setFont(counter.getFont().deriveFont((float) (counter.getFont().getSize() + 5))); setLayout(new GridBagLayout()); setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); GridBagConstraints constraints = new GridBagConstraints(); JLabel messageLabel = new JLabel( GuiActivator.getResources().getI18NString("service.gui.security.SECURITY_ALERT")); messageLabel.setForeground(Color.WHITE); constraints.anchor = GridBagConstraints.CENTER; constraints.fill = GridBagConstraints.NONE; constraints.gridx = 0; constraints.gridy = 0; add(messageLabel, constraints); constraints.anchor = GridBagConstraints.CENTER; constraints.fill = GridBagConstraints.NONE; constraints.gridx = 0; constraints.gridy = 1; add(counter, constraints); ZrtpControl zrtpControl = null; if (securityControl instanceof ZrtpControl) zrtpControl = (ZrtpControl) securityControl; long initialSeconds = 0; if (zrtpControl != null) initialSeconds = zrtpControl.getTimeoutValue(); c.setTimeInMillis(initialSeconds); counter.setText(format.format(c.getTime())); if (initialSeconds > 0) timer.schedule( new TimerTask() { @Override public void run() { if (c.getTimeInMillis() - 1000 > 0) { c.add(Calendar.SECOND, -1); counter.setText(format.format(c.getTime())); } } }, 1000, 1000); }
private static boolean keepCurrent(Calendar asOfDate) { boolean keepCurrent = false; int monthNow = asOfDate.get(Calendar.MONTH); // March, June, September, and December are expiration months boolean isExpirationMonth = ((monthNow + 1) % 3 == 0); if (isExpirationMonth) { Calendar volumeShiftDate = (Calendar) asOfDate.clone(); // Find first Friday volumeShiftDate.set(Calendar.DAY_OF_MONTH, 1); while (volumeShiftDate.get(Calendar.DAY_OF_WEEK) != Calendar.FRIDAY) { volumeShiftDate.add(Calendar.DAY_OF_MONTH, 1); } // Shift to third Friday volumeShiftDate.add(Calendar.WEEK_OF_MONTH, 2); // Finally, find the day before second Friday volumeShiftDate.add(Calendar.DAY_OF_MONTH, -8); if (asOfDate.before(volumeShiftDate)) { keepCurrent = true; } } return keepCurrent; }
public ArtifactView(DataProvider provider) { this( new Artifact( new DateTime( Calendar.getInstance().getTime(), DateFormat.getDateInstance(DateFormat.MEDIUM)), new DateTime( Calendar.getInstance().getTime(), DateFormat.getDateInstance(DateFormat.MEDIUM)), "", "", "", "", ""), provider); }
public void testDate() { final Date date = Vba.date(); assertNotNull(date); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); assertEquals(0, calendar.get(Calendar.HOUR_OF_DAY)); assertEquals(0, calendar.get(Calendar.MILLISECOND)); }
/** * 현재날짜를 dateType형식으로 보여준다. ForExample : yyyy yyyy-MM yyyy-MM-dd yyyyMMdd * * @param dateType * @return */ public static String getCurrentDateByType(String dateType) { Calendar now = Calendar.getInstance(); long lCurDate = now.getTimeInMillis(); SimpleDateFormat sdf = new SimpleDateFormat(dateType); String sCurrentDate = sdf.format(lCurDate); return sCurrentDate; }
// 현재날짜 public static String getCurrentDate() { Calendar now = Calendar.getInstance(); long lCurDate = now.getTimeInMillis(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); String sCurrentDate = sdf.format(lCurDate); return sCurrentDate; }
static void printResult(Calendar from, Calendar to) { Date fromDate = from.getTime(); Date toDate = to.getTime(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); System.out.print(sdf.format(fromDate) + " ~ " + sdf.format(toDate) + ":"); System.out.println(paycheckCount(from, to)); }
public static String getZoneOffset(Date date, Locale locale) { String dateStr; Calendar cal = Calendar.getInstance(locale); cal.setTime(date); int offset = cal.get(Calendar.ZONE_OFFSET) / (60 * 60 * 1000); if (offset > -1) dateStr = " +" + offset + "h"; else dateStr = " -" + offset + "h"; return dateStr; }
/** * Finds the occurrence of the events in the next months * * @param cal the calendar object * @param lastDay if <tt>true</tt> it will return the last day of the month * @param period the number of months to add * @return the calendar object with set date */ private Calendar incrementMonths(Calendar cal, boolean lastDay, int period) { int dayOfMonth = patternSpecific1; cal.set(Calendar.DAY_OF_MONTH, 1); cal.add(Calendar.MONTH, period); if (lastDay || (cal.getActualMaximum(Calendar.DAY_OF_MONTH) < dayOfMonth)) dayOfMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH); cal.set(Calendar.DAY_OF_MONTH, dayOfMonth); return cal; }
public static void main(String[] av) { /** Today's date */ Calendar now = Calendar.getInstance(); /* Do "DateFormat" using "simple" format. */ SimpleDateFormat formatter = new SimpleDateFormat("E yyyy/MM/dd 'at' hh:mm:ss a zzz"); System.out.println("It is now " + formatter.format(now.getTime())); // Add a "# of years" increment to the existing Calendar object now.add(Calendar.YEAR, -2); System.out.println("Two years ago was " + formatter.format(now.getTime())); }
static Date parseDate(String s) { Calendar c = new GregorianCalendar(TimeZone.getTimeZone("UTC")); int year = Integer.parseInt(s.substring(0, 4)); int month = Integer.parseInt(s.substring(4, 6)) - 1; int date = Integer.parseInt(s.substring(6, 8)); int hour = Integer.parseInt(s.substring(8, 10)); int minute = Integer.parseInt(s.substring(10, 12)); int second = Integer.parseInt(s.substring(12, 14)); c.set(year, month, date, hour, minute, second); return c.getTime(); }
/* paint() - get current time and draw (centered) in Component. */ public void paint(Graphics g) { Calendar myCal = Calendar.getInstance(); StringBuffer sb = new StringBuffer(); sb.append(tf.format(myCal.get(Calendar.HOUR))); sb.append(':'); sb.append(tflz.format(myCal.get(Calendar.MINUTE))); sb.append(':'); sb.append(tflz.format(myCal.get(Calendar.SECOND))); String s = sb.toString(); FontMetrics fm = getFontMetrics(getFont()); int x = (getSize().width - fm.stringWidth(s)) / 2; // System.out.println("Size is " + getSize()); g.drawString(s, x, 10); }
/** * For the contracts whose volume shifts from the front contract to the back contract on the day * preceding the 2nd Friday of expiration month of the front contract. */ public static String getMostLiquid(Calendar asOfDate) { int monthNow = asOfDate.get(Calendar.MONTH); int yearNow = asOfDate.get(Calendar.YEAR); int mostLiquidYear = yearNow; int mostLiquidMonth = frontMonths.get(monthNow); // special case with December if (monthNow == Calendar.DECEMBER) { mostLiquidYear = yearNow + 1; } if (keepCurrent(asOfDate)) { mostLiquidMonth = monthNow; mostLiquidYear = yearNow; } Calendar mostLiquidDate = Calendar.getInstance(); mostLiquidDate.set(Calendar.DAY_OF_MONTH, 1); mostLiquidDate.set(Calendar.MONTH, mostLiquidMonth); mostLiquidDate.set(Calendar.YEAR, mostLiquidYear); SimpleDateFormat df = new SimpleDateFormat("yyyyMM"); return df.format(mostLiquidDate.getTime()); }
public void testWeekdayName() { // If Sunday (1) is the first day of the week // then day 1 is Sunday, // then day 2 is Monday, // and day 7 is Saturday assertEquals("Sunday", Vba.weekdayName(1, false, 1)); assertEquals("Monday", Vba.weekdayName(2, false, 1)); assertEquals("Saturday", Vba.weekdayName(7, false, 1)); assertEquals("Sat", Vba.weekdayName(7, true, 1)); // If Monday (2) is the first day of the week // then day 1 is Monday, // and day 7 is Sunday assertEquals("Monday", Vba.weekdayName(1, false, 2)); assertEquals("Sunday", Vba.weekdayName(7, false, 2)); // Use weekday start from locale. Test for the 2 most common. switch (Calendar.getInstance().getFirstDayOfWeek()) { case Calendar.SUNDAY: assertEquals("Sunday", Vba.weekdayName(1, false, 0)); assertEquals("Monday", Vba.weekdayName(2, false, 0)); assertEquals("Saturday", Vba.weekdayName(7, false, 0)); assertEquals("Sat", Vba.weekdayName(7, true, 0)); break; case Calendar.MONDAY: assertEquals("Monday", Vba.weekdayName(1, false, 0)); assertEquals("Tuesday", Vba.weekdayName(2, false, 0)); assertEquals("Sunday", Vba.weekdayName(7, false, 0)); assertEquals("Sun", Vba.weekdayName(7, true, 0)); break; } }
/** Output postscript header to PrintStream, including helper macros. */ private void Header() { m_printstream.println("%!PS-Adobe-3.0 EPSF-3.0"); m_printstream.println( "%%BoundingBox: 0 0 " + xScale(m_extent.width) + " " + yScale(m_extent.height)); m_printstream.println("%%CreationDate: " + Calendar.getInstance().getTime()); m_printstream.println("/Oval { % x y w h filled"); m_printstream.println("gsave"); m_printstream.println("/filled exch def /h exch def /w exch def /y exch def /x exch def"); m_printstream.println("x w 2 div add y h 2 div sub translate"); m_printstream.println("1 h w div scale"); m_printstream.println("filled {0 0 moveto} if"); m_printstream.println("0 0 w 2 div 0 360 arc"); m_printstream.println("filled {closepath fill} {stroke} ifelse grestore} bind def"); m_printstream.println("/Rect { % x y w h filled"); m_printstream.println("/filled exch def /h exch def /w exch def /y exch def /x exch def"); m_printstream.println("newpath "); m_printstream.println("x y moveto"); m_printstream.println("w 0 rlineto"); m_printstream.println("0 h neg rlineto"); m_printstream.println("w neg 0 rlineto"); m_printstream.println("closepath"); m_printstream.println("filled {fill} {stroke} ifelse} bind def"); m_printstream.println("%%BeginProlog\n%%EndProlog"); m_printstream.println("%%Page 1 1"); setFont(null); // set to default setColor(null); // set to default setStroke(null); // set to default }
/** * Gets the value of this datetime as a milliseconds value for {@link java.sql.Date}. * * @param zone time zone in which to generate a time value for */ public long getJdbcDate(TimeZone zone) { Calendar cal = getCalendar(DateTimeUtil.gmtZone); cal.setTimeInMillis(getDateValue()); int year = cal.get(Calendar.YEAR); int doy = cal.get(Calendar.DAY_OF_YEAR); cal.clear(); cal.setTimeZone(zone); cal.set(Calendar.YEAR, year); cal.set(Calendar.DAY_OF_YEAR, doy); return cal.getTimeInMillis(); }
protected Calendar formatStringAsCalendar(String calendarString) { try { String dateOnly = calendarString.substring(0, 19); String timeZoneStr = calendarString.substring(20, calendarString.length()); TimeZone timeZone = TimeZone.getTimeZone(timeZoneStr); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); simpleDateFormat.setTimeZone(timeZone); Date referenceDate = simpleDateFormat.parse(dateOnly.toUpperCase()); Calendar referenceCalendar = new GregorianCalendar(timeZone); referenceCalendar.setTime(referenceDate); simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z"); simpleDateFormat.setTimeZone(timeZone); return referenceCalendar; } catch (ParseException p) { throw new TestErrorException("Could not parse Calendar String: " + calendarString); } }
/** Typical main method ("main program") declaration */ public static void main(String[] av) { Locale l1 = new Locale("en", "US"), l2 = new Locale("es", "ES"); // Create a Date object for May 5, 1986 Calendar c = Calendar.getInstance(); c.set(1986, 04, 05); // May 5, 1986 Date d1 = c.getTime(); // Create a Date object for today. Date d2 = new Date(); // today DateFormat df_us = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, l1), df_sp = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, l2); System.out.println("Date d1 for US is " + df_us.format(d1)); System.out.println("Date d1 for Spain is " + df_sp.format(d1)); System.out.println("Date d2 is " + df_us.format(d2)); }
private void saveStartProjects(File file) throws IOException { Calendar cal = Calendar.getInstance(); try (PrintWriter out = new PrintWriter(file, "windows-1251")) { for (Region region : this.regions) { cal.setTime(region.start); out.write("\""); out.write(region.filial + " " + region.name); out.write("\";;;"); int skeepCells = (cal.get(Calendar.YEAR) - 2017) * 5 + (cal.get(Calendar.MONDAY) % 4); for (int i = 0; i < skeepCells; i++) { out.write(";"); } out.println("X"); } } }
public void testDateAdd() { assertEquals("2008/04/24 19:10:45", SAMPLE_DATE); // 2008-02-01 0:00:00 Calendar calendar = Calendar.getInstance(); calendar.set(2007, 1 /* 0-based! */, 1, 0, 0, 0); final Date feb2007 = calendar.getTime(); assertEquals("2007/02/01 00:00:00", feb2007); assertEquals("2008/04/24 19:10:45", Vba.dateAdd("yyyy", 0, SAMPLE_DATE)); assertEquals("2009/04/24 19:10:45", Vba.dateAdd("yyyy", 1, SAMPLE_DATE)); assertEquals("2006/04/24 19:10:45", Vba.dateAdd("yyyy", -2, SAMPLE_DATE)); // partial years interpolate final Date sampleDatePlusTwoPointFiveYears = Vba.dateAdd("yyyy", 2.5, SAMPLE_DATE); if (isPST) { // Only run test in PST, because test would produce different // results if start and end are not both in daylight savings time. final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.US); final String dateString = dateFormat.format(sampleDatePlusTwoPointFiveYears); // We allow "2010/10/24 07:10:45" for computers that have an out of // date timezone database. 2010/10/24 is in daylight savings time, // but was not according to the old rules. assertTrue( "Got " + dateString, dateString.equals("2010/10/24 06:40:45") || dateString.equals("2010/10/24 07:10:45")); } assertEquals("2009/01/24 19:10:45", Vba.dateAdd("q", 3, SAMPLE_DATE)); // partial months are interesting! assertEquals("2008/06/24 19:10:45", Vba.dateAdd("m", 2, SAMPLE_DATE)); assertEquals("2007/01/01 00:00:00", Vba.dateAdd("m", -1, feb2007)); assertEquals("2007/03/01 00:00:00", Vba.dateAdd("m", 1, feb2007)); assertEquals("2007/02/08 00:00:00", Vba.dateAdd("m", .25, feb2007)); // feb 2008 is a leap month, so a quarter month is 7.25 days assertEquals("2008/02/08 06:00:00", Vba.dateAdd("m", 12.25, feb2007)); assertEquals("2008/05/01 19:10:45", Vba.dateAdd("y", 7, SAMPLE_DATE)); assertEquals("2008/05/02 01:10:45", Vba.dateAdd("y", 7.25, SAMPLE_DATE)); assertEquals("2008/04/24 23:10:45", Vba.dateAdd("h", 4, SAMPLE_DATE)); assertEquals("2008/04/24 20:00:45", Vba.dateAdd("n", 50, SAMPLE_DATE)); assertEquals("2008/04/24 19:10:36", Vba.dateAdd("s", -9, SAMPLE_DATE)); }
/** * Finds the occurrence of the events in the next months * * @param startDate the start date if the calendar item * @param dayOfWeekInMonth the number of week days occurrences * @return the date of the next occurrence */ private Date getMonthNStartDate(Date startDate, int dayOfWeekInMonth) { Calendar cal = Calendar.getInstance(); cal.setTime(startDate); if (dayOfWeekInMonth == -1) { Date result = null; cal.set(Calendar.DAY_OF_WEEK_IN_MONTH, dayOfWeekInMonth); for (int day : allowedDaysOfWeek) { cal.set(Calendar.DAY_OF_WEEK, day); if (result == null || result.before(cal.getTime())) result = cal.getTime(); } return result; } else while (dayOfWeekInMonth > 0) { int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); if (allowedDaysOfWeek.contains(dayOfWeek)) dayOfWeekInMonth--; if (dayOfWeekInMonth > 0) cal.add(Calendar.DAY_OF_MONTH, 1); } return cal.getTime(); }
/** * Parses a formatted time string into a Date. * * @param s The string, in the form YYYYMMDDHHMMSS. * @return The Date object. * @throws TextParseExcetption The string was invalid. */ public static Date parse(String s) throws TextParseException { if (s.length() != 14) { throw new TextParseException("Invalid time encoding: " + s); } Calendar c = new GregorianCalendar(TimeZone.getTimeZone("UTC")); c.clear(); try { int year = Integer.parseInt(s.substring(0, 4)); int month = Integer.parseInt(s.substring(4, 6)) - 1; int date = Integer.parseInt(s.substring(6, 8)); int hour = Integer.parseInt(s.substring(8, 10)); int minute = Integer.parseInt(s.substring(10, 12)); int second = Integer.parseInt(s.substring(12, 14)); c.set(year, month, date, hour, minute, second); } catch (NumberFormatException e) { throw new TextParseException("Invalid time encoding: " + s); } return c.getTime(); }
/** * Map millis and timeZone to its component fields. * * <p>Bits0: ------------------------------------------------ Field Num Bits Range Loc * ------------------------------------------------ Year 16 short 16-31 Millis 16 short 0-15 * * <p>Bits1: ------------------------------------------------ Field Num Bits Range Loc * ------------------------------------------------ Daylight 1 0-1 29-29 Month 4 1-12 25-28 Day 5 * 1-31 20-24 Hour 5 0-23 15-19 Minutes 6 0-59 9-14 Seconds 6 0-59 3-8 Weekday 3 0-6 0-2 * ------------------------------------------------ */ private void millisToFields() { // init a calendar with timeZone and millis Calendar calendar = new GregorianCalendar(timeZone); Date date = new Date(millis); calendar.setTime(date); // set year bits int x = calendar.get(Calendar.YEAR); bits0 |= ((x & 0xFFFF) << 16); // set millisecond bits x = calendar.get(Calendar.MILLISECOND); bits0 |= ((x & 0xFFFF) << 0); // set month bits x = calendar.get(Calendar.MONTH) + 1; bits1 |= ((x & 0x0F) << 25); // set day bits x = calendar.get(Calendar.DAY_OF_MONTH); bits1 |= ((x & 0x1F) << 20); // set hour bits x = calendar.get(Calendar.HOUR_OF_DAY); bits1 |= ((x & 0x1F) << 15); // set minute bits x = calendar.get(Calendar.MINUTE); bits1 |= ((x & 0x3F) << 9); // set seconds bits x = calendar.get(Calendar.SECOND); bits1 |= ((x & 0x3F) << 3); // set weekday x = calendar.get(Calendar.DAY_OF_WEEK) - 1; bits1 |= ((x & 0x07) << 0); // set daylight bit if (timeZone.inDaylightTime(date)) bits1 |= (0x01 << 29); }
public DeliveryViewModel(String urlPrefix, String locationName, Calendar date) { if (date == null) { isDefaultDate = true; this.date = Calendar.getInstance(); } else { this.date = date; } this.locationName = locationName; this.urlPrefix = urlPrefix; menuMap = new HashMap<String, DayMenuViewModel>(); }