GregorianCalendar c = new GregorianCalendar(); c.setTime(yourDate); XMLGregorianCalendar date2 = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
Calendar startCalendar = new GregorianCalendar(); startCalendar.setTime(startDate); Calendar endCalendar = new GregorianCalendar(); endCalendar.setTime(endDate); int diffYear = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR); int diffMonth = diffYear * 12 + endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);
Calendar cal = new GregorianCalendar(); SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); dateFormat.setTimeZone(cal.getTimeZone()); System.out.println(dateFormat.format(cal.getTime()));
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // replace with your start date string Date d = df.parse("2008-04-16 00:05:05"); Calendar gc = new GregorianCalendar(); gc.setTime(d); gc.add(Calendar.HOUR, 2); Date d2 = gc.getTime();
Calendar c = new GregorianCalendar(); c.set(Calendar.HOUR_OF_DAY, 12); c.set(Calendar.MINUTE, 58); c.set(Calendar.SECOND, 15); Date d = c.getTime(); Calendar c = new GregorianCalendar(); c.setTime(d); if (c.get(Calendar.SECOND) >= 30) c.add(Calendar.MINUTE, 1); c.set(Calendar.SECOND, 0); return c.getTime(); Calendar c = new GregorianCalendar(); c.setTime(d); if (c.get(Calendar.MINUTE) >= 30) c.add(Calendar.HOUR, 1); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); return c.getTime();
public static void main(String[] args) { Calendar calendar = new GregorianCalendar(); System.out.println("the date is : " + calendar.getTime().getTime()); System.out.println("the timezoneOffset is : " + getTimezoneOffset()); String postDate = toPostDate(calendar.getTime()); System.out.println("the post date is : " + postDate); try { System.out.println("post date to date : " + parse(postDate, getTimezoneOffset()).getTime().getTime()); } catch (ParseException e) { e.printStackTrace(); } } }
Calendar mydate = new GregorianCalendar(); String mystring = "January 2, 2010"; Date thedate = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse(mystring); mydate.setTime(thedate); //breakdown System.out.println("mydate -> "+mydate); System.out.println("year -> "+mydate.get(Calendar.YEAR)); System.out.println("month -> "+mydate.get(Calendar.MONTH)); System.out.println("dom -> "+mydate.get(Calendar.DAY_OF_MONTH)); System.out.println("dow -> "+mydate.get(Calendar.DAY_OF_WEEK)); System.out.println("hour -> "+mydate.get(Calendar.HOUR)); System.out.println("minute -> "+mydate.get(Calendar.MINUTE)); System.out.println("second -> "+mydate.get(Calendar.SECOND)); System.out.println("milli -> "+mydate.get(Calendar.MILLISECOND)); System.out.println("ampm -> "+mydate.get(Calendar.AM_PM)); System.out.println("hod -> "+mydate.get(Calendar.HOUR_OF_DAY));
import java.util.GregorianCalendar; import javax.xml.datatype.DatatypeFactory; import javax.xml.datatype.XMLGregorianCalendar; public class DateTest { public static void main(final String[] args) throws Exception { GregorianCalendar gcal = new GregorianCalendar(); XMLGregorianCalendar xgcal = DatatypeFactory.newInstance() .newXMLGregorianCalendar(gcal); System.out.println(xgcal); } }
public Date getTimeAfter(Date afterTime) { Calendar cl = new java.util.GregorianCalendar(getTimeZone()); cl.setTime(afterTime); cl.set(Calendar.MILLISECOND, 0); if(cl.get(Calendar.YEAR) > 2999) { // prevent endless loop... return null; int t = 0; int sec = cl.get(Calendar.SECOND); int min = cl.get(Calendar.MINUTE); sec = seconds.first(); min++; cl.set(Calendar.MINUTE, min); cl.set(Calendar.SECOND, sec); min = cl.get(Calendar.MINUTE); mon = 1; tmon = 3333; // ensure test of mon != tmon further below fails cl.add(Calendar.YEAR, 1); return cl.getTime();
import java.util.GregorianCalendar; public class RandomDateOfBirth { public static void main(String[] args) { GregorianCalendar gc = new GregorianCalendar(); int year = randBetween(1900, 2010); gc.set(gc.YEAR, year); int dayOfYear = randBetween(1, gc.getActualMaximum(gc.DAY_OF_YEAR)); gc.set(gc.DAY_OF_YEAR, dayOfYear); System.out.println(gc.get(gc.YEAR) + "-" + (gc.get(gc.MONTH) + 1) + "-" + gc.get(gc.DAY_OF_MONTH)); } public static int randBetween(int start, int end) { return start + (int)Math.round(Math.random() * (end - start)); } }
Calendar calendar = new GregorianCalendar(); calendar.setTimeZone(this.timeZone); calendar.setTime(date); calendar.set(Calendar.MILLISECOND, 0); long originalTimestamp = calendar.getTimeInMillis(); doNext(calendar, calendar.get(Calendar.YEAR)); calendar.add(Calendar.SECOND, 1); doNext(calendar, calendar.get(Calendar.YEAR)); return calendar.getTime();
Calendar tmpFrom = new GregorianCalendar(); if (timezone != null) { tmpFrom.setTimeZone(TimeZone.getTimeZone(timezone)); tmpFrom.setTime(sdf.parse(fromTo[0])); Calendar tmpTo = new GregorianCalendar(); if (timezone != null) { tmpTo.setTimeZone(TimeZone.getTimeZone(timezone)); tmpTo.setTime(sdf.parse(fromTo[1])); Date from = tmpFrom.getTime(); tmpTo.add(Calendar.DAY_OF_YEAR, 1); if ((tmpFrom.get(Calendar.MONTH) > tmpTo.get(Calendar.MONTH)) && (tmpFrom.get(Calendar.YEAR) == tmpTo.get(Calendar.YEAR))) { tmpTo.add(Calendar.YEAR, 1); tmpFrom = new GregorianCalendar(); if (timezone != null) { tmpFrom.setTimeZone(TimeZone.getTimeZone(timezone)); from = tmpFrom.getTime(); tmpTo = new GregorianCalendar(); if (timezone != null) { tmpTo.setTimeZone(TimeZone.getTimeZone(timezone)); Calendar c = new GregorianCalendar();
/** * Sets the Date header for the HTTP response * * @param response * HTTP response */ private static void setDateHeader(FullHttpResponse response) { SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US); dateFormatter.setTimeZone(TimeZone.getTimeZone(HTTP_DATE_GMT_TIMEZONE)); Calendar time = new GregorianCalendar(); response.headers().set(DATE, dateFormatter.format(time.getTime())); }
/** * 描述:获取本月第一天. * * @param format the format * @return String String类型日期时间 */ public static String getFirstDayOfMonth(String format) { String strDate = null; try { Calendar c = new GregorianCalendar(); SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format); //当前月的第一天 c.set(GregorianCalendar.DAY_OF_MONTH, 1); strDate = mSimpleDateFormat.format(c.getTime()); } catch (Exception e) { e.printStackTrace(); } return strDate; }
Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, 2014); cal.set(Calendar.DAY_OF_YEAR, 1); Date start = cal.getTime(); //set date to last day of 2014 cal.set(Calendar.YEAR, 2014); cal.set(Calendar.MONTH, 11); // 11 = december cal.set(Calendar.DAY_OF_MONTH, 31); // new years eve Date end = cal.getTime(); //Iterate through the two dates GregorianCalendar gcal = new GregorianCalendar(); gcal.setTime(start); while (gcal.getTime().before(end)) { gcal.add(Calendar.DAY_OF_YEAR, 1); //Do Something ... }
Long gmtTime =1317951113613L; // 2.32pm NZDT Long timezoneAlteredTime = 0L; if (offset != 0L) { int multiplier = (offset*60)*(60*1000); timezoneAlteredTime = gmtTime + multiplier; } else { timezoneAlteredTime = gmtTime; } Calendar calendar = new GregorianCalendar(); calendar.setTimeInMillis(timezoneAlteredTime); DateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z"); formatter.setCalendar(calendar); formatter.setTimeZone(TimeZone.getTimeZone(timeZone)); String newZealandTime = formatter.format(calendar.getTime());
Calendar c = new GregorianCalendar(); c.set(Calendar.HOUR_OF_DAY, 0); //anything 0 - 23 c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); Date d1 = c.getTime(); //the midnight, that's the first second of the day.
protected void handleHoliday(Calendar c, boolean resetTime) { if (!holidays.isEmpty()) { Date current = c.getTime(); for (TimePeriod holiday : holidays) { // check each holiday if it overlaps current date and break after first match if (current.after(holiday.getFrom()) && current.before(holiday.getTo())) { Calendar tmp = new GregorianCalendar(); tmp.setTime(holiday.getTo()); Calendar tmp2 = new GregorianCalendar(); tmp2.setTime(current); tmp2.set(Calendar.HOUR_OF_DAY, 0); tmp2.set(Calendar.MINUTE, 0); tmp2.set(Calendar.SECOND, 0); tmp2.set(Calendar.MILLISECOND, 0); long difference = tmp.getTimeInMillis() - tmp2.getTimeInMillis(); c.add(Calendar.HOUR_OF_DAY, (int) (difference/HOUR_IN_MILLIS)); handleWeekend(c, resetTime); break; } } } }
/** * Generates a random birthday between two ages. * * @param minAge * the minimal age * @param maxAge * the maximal age * @return a random birthday between {@code minAge} and {@code maxAge} years ago. * @throws IllegalArgumentException * if the {@code maxAge} is lower than {@code minAge}. */ public Date birthday(int minAge, int maxAge) { int currentYear = Calendar.getInstance().get(Calendar.YEAR); Calendar from = new GregorianCalendar(currentYear - maxAge, 0, 1); Calendar to = new GregorianCalendar(currentYear - minAge, 11, 31); return between(from.getTime(), to.getTime()); }