Java Code Examples for java.util.Calendar
The following code examples are extracted from open source projects. You can click to
vote up the examples that are useful to you.
Example 1
From project Absolute-Android-RSS, under directory /src/com/AA/Other/.
Source file: DateFunctions.java

/** * Creates a Calendar object based off a string with this format <Day name:3 letters>, DD <Month name: 3 letters> YYYY HH:MM:SS +0000 * @param date - The string we are to convert to a calendar date * @return - The calendar date object that this string represents */ public static Calendar makeDate(String date){ Date d=null; Calendar calendar=Calendar.getInstance(); try { SimpleDateFormat format=new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z"); d=format.parse(date); calendar.setTime(d); } catch ( ParseException e) { e.printStackTrace(); } return calendar; }
Example 2
From project 2Degrees-Toolbox, under directory /Toolbox/src/biz/shadowservices/DegreesToolbox/.
Source file: DateFormatters.java

public static boolean isSameDay(Date date1,Date date2){ if (date1 == null || date2 == null) { throw new IllegalArgumentException("The date must not be null"); } Calendar cal1=Calendar.getInstance(); cal1.setTime(date1); Calendar cal2=Calendar.getInstance(); cal2.setTime(date2); return isSameDay(cal1,cal2); }
Example 3
From project 3Dto2DApplet, under directory /src/java/nl/dannyarends/generator/model/.
Source file: AbstractGenerator.java

public Map<String,Object> createArguments(){ Map<String,Object> args=new TreeMap<String,Object>(); Calendar calendar=Calendar.getInstance(); args.put("year",calendar.get(Calendar.YEAR)); DateFormat formatter=new SimpleDateFormat("MMMM d, yyyy, HH:mm:ss",Locale.US); args.put("datetime",formatter.format(new Date())); formatter=new SimpleDateFormat("MMMM d, yyyy",Locale.US); args.put("date",formatter.format(new Date())); return args; }
Example 4
From project activejdbc, under directory /javalite-common/src/main/java/org/javalite/common/.
Source file: Convert.java

private static java.sql.Date utilDate2sqlDate(long time){ Calendar calendar=new GregorianCalendar(); calendar.setTimeInMillis(time); calendar.set(Calendar.HOUR_OF_DAY,0); calendar.set(Calendar.MINUTE,0); calendar.set(Calendar.SECOND,0); calendar.set(Calendar.MILLISECOND,0); return new java.sql.Date(calendar.getTimeInMillis()); }
Example 5
/** * Get the real time from the server * @author Lathanael * @param gmt The wanted GMT offset * @return serverTime Represents the time read from the server */ public static Date getServerRealTime(final String gmt){ Date serverTime; final TimeZone tz=TimeZone.getTimeZone(gmt); final Calendar cal=Calendar.getInstance(tz); cal.setTime(new Date()); serverTime=cal.getTime(); return serverTime; }
Example 6
From project AdServing, under directory /modules/server/adserver/src/main/java/net/mad/ads/server/utils/request/.
Source file: RequestHelper.java

private static void addTimeCondition(HttpServletRequest request,AdRequest adRequest){ String strOffSet=request.getParameter(RequestHelper.offset); int offset=Integer.parseInt(strOffSet); offset=offset * 60000; String[] ids=TimeZone.getAvailableIDs(offset); Calendar temp=Calendar.getInstance(); temp.setTimeZone(TimeZone.getTimeZone(ids[0])); DateFormat dateFormat=new SimpleDateFormat("yyyyMMdd"); DateFormat timeFormat=new SimpleDateFormat("HHmm"); adRequest.setTime(timeFormat.format(temp.getTime())); adRequest.setDate(dateFormat.format(temp.getTime())); adRequest.setDay(Day.getDayForJava(temp.get(Calendar.DAY_OF_WEEK))); }
Example 7
From project aether-core, under directory /aether-impl/src/test/java/org/eclipse/aether/internal/impl/.
Source file: DefaultUpdatePolicyAnalyzerTest.java

@Test public void testIsUpdateRequired_PolicyDaily() throws Exception { Calendar cal=Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY,0); cal.set(Calendar.MINUTE,0); cal.set(Calendar.SECOND,0); cal.set(Calendar.MILLISECOND,0); long localMidnight=cal.getTimeInMillis(); String policy=RepositoryPolicy.UPDATE_POLICY_DAILY; assertEquals(true,analyzer.isUpdatedRequired(session,Long.MIN_VALUE,policy)); assertEquals(false,analyzer.isUpdatedRequired(session,Long.MAX_VALUE,policy)); assertEquals(false,analyzer.isUpdatedRequired(session,localMidnight + 0,policy)); assertEquals(false,analyzer.isUpdatedRequired(session,localMidnight + 1,policy)); assertEquals(true,analyzer.isUpdatedRequired(session,localMidnight - 1,policy)); }
Example 8
From project agile, under directory /agile-storage/src/main/java/org/headsupdev/agile/storage/.
Source file: DurationWorkedUtil.java

public static Double getCurrentUserVelocity(User user){ Calendar cal=Calendar.getInstance(); cal.add(Calendar.WEEK_OF_YEAR,-1); List<DurationWorked> worked=getDurationWorkedForUser(user,cal.getTime(),new Date()); Double velocity=calculateVelocity(worked,user); return velocity; }
Example 9
From project agileBase, under directory /gtpb_server/src/com/gtwm/pb/model/manageSchema/fields/.
Source file: DateFieldDefn.java

@Transient public Calendar getDefault(){ if (this.getDefaultToNow()) { Calendar now=new GregorianCalendar(); return now; } else { return null; } }
Example 10
From project Airports, under directory /src/com/nadmm/airports/utils/.
Source file: SolarCalculator.java

private Calendar getTimeAsCalendar(double time,Calendar date){ int hours=(int)Math.floor(time); int minutes=(int)((time - hours) * 60); if (minutes == 60) { minutes=0; ++hours; } Calendar result=(Calendar)date.clone(); result.set(Calendar.HOUR_OF_DAY,hours); result.set(Calendar.MINUTE,minutes); result.set(Calendar.SECOND,0); return result; }
Example 11
From project ajah, under directory /ajah-event-http/src/main/java/com/ajah/log/http/request/.
Source file: RequestEvent.java

/** * Invoked when the request is completed, establishing the end time. * @see com.ajah.event.Event#complete() */ @Override public void complete(){ this.end=System.currentTimeMillis(); final Calendar cal=Calendar.getInstance(); cal.setTimeInMillis(this.start); this.year=cal.get(Calendar.YEAR); this.month=cal.get(Calendar.MONTH) + (this.year * 12); this.day=cal.get(Calendar.DAY_OF_YEAR) + (this.month * 31); this.hour=cal.get(Calendar.HOUR_OF_DAY) + (this.day * 24); this.minute=cal.get(Calendar.MINUTE) + (this.hour * 60); }
Example 12
/** * Iterate from startMillis to endMillis (including endMillis) in increments of incrementCount * incrementType * @param startMillis * @param endMillis * @param incrementType * @param incrementCount * @param iterator */ public static void iterateByTime(long startMillis,long endMillis,int incrementType,int incrementCount,DateIterator iterator){ Calendar start=Calendar.getInstance(); start.setTimeInMillis(startMillis); Calendar end=Calendar.getInstance(); end.setTimeInMillis(endMillis); while (!start.after(end)) { iterator.see(start.getTimeInMillis()); start.add(incrementType,incrementCount); } }
Example 13
/** * Sets the time of last modification of the entry. * @time the time of last modification of the entry. */ public void setTime(long time){ Calendar cal=getCalendar(); synchronized (cal) { Date d=new Date(time); cal.setTime(d); dostime=(cal.get(Calendar.YEAR) - 1980 & 0x7f) << 25 | (cal.get(Calendar.MONTH) + 1) << 21 | (cal.get(Calendar.DAY_OF_MONTH)) << 16 | (cal.get(Calendar.HOUR_OF_DAY)) << 11 | (cal.get(Calendar.MINUTE)) << 5 | (cal.get(Calendar.SECOND)) >> 1; } this.known|=KNOWN_TIME; }
Example 14
From project ALP, under directory /workspace/alp-reporter-fe/src/main/java/com/lohika/alp/reporter/fe/controller/.
Source file: TestController.java

private void setDefaultPeriod(TestFilter filter){ Calendar cal=Calendar.getInstance(); if (filter.getFrom() == null || filter.getTill() == null) { filter.setTill(cal.getTime()); cal.add(Calendar.DATE,-1); filter.setFrom(cal.getTime()); } }
Example 15
From project Amoeba-for-Aladdin, under directory /src/java/com/meidusa/amoeba/mysql/net/packet/.
Source file: PacketUtil.java

public static Time readTime(MysqlPacketBuffer intoBuf){ intoBuf.readByte(); intoBuf.readByte(); intoBuf.readLong(); int hour=intoBuf.readByte(); int minute=intoBuf.readByte(); int second=intoBuf.readByte(); Calendar cal=(Calendar)ThreadLocalMap.get(StaticString.CALENDAR); cal.set(0,0,0,hour,minute,second); return new Time(cal.getTimeInMillis()); }
Example 16
From project amplafi-sworddance, under directory /src/main/java/com/sworddance/scheduling/.
Source file: TimeServerImpl.java

public Calendar getZeroedCalendar(){ Calendar zero=getApproximateCalendar(); zero.set(0,0,0,0,0); zero.set(Calendar.SECOND,0); return zero; }
Example 17
From project and-bible, under directory /AndBible/src/net/bible/service/readingplan/.
Source file: OneDaysReadingsDto.java

/** * get a string representing the date this reading is planned for */ public String getReadingDateString(){ String dateString=""; Date startDate=mReadingPlanInfoDto.getStartdate(); if (startDate != null) { Calendar cal=Calendar.getInstance(); cal.setTime(startDate); cal.add(Calendar.DAY_OF_MONTH,mDay - 1); dateString=SimpleDateFormat.getDateInstance().format(cal.getTime()); } return dateString; }
Example 18
/** * Writes a log entry into the log file of the respective ontology. * @param type The type of the log entry. * @param text The text of the log entry. */ public void log(String type,String text){ Calendar c=Calendar.getInstance(); long timestamp=System.currentTimeMillis(); c.setTimeInMillis(timestamp); String year=c.get(Calendar.YEAR) + ""; String month=makeString(c.get(Calendar.MONTH) + 1,2); String day=makeString(c.get(Calendar.DAY_OF_MONTH),2); String hour=makeString(c.get(Calendar.HOUR_OF_DAY),2); String min=makeString(c.get(Calendar.MINUTE),2); String sec=makeString(c.get(Calendar.SECOND),2); String millis=makeString(c.get(Calendar.MILLISECOND),3); String dateTime=year + "-" + month+ "-"+ day+ " "+ hour+ ":"+ min+ ":"+ sec+ "."+ millis; String session=makeString(sessionID,4); String un; if (username == null || username.equals("")) { un=""; } else { un=" '" + username + "'"; } String dir="logs"; String fn=fileName; if (fileName.indexOf("/") > -1) { dir=fileName.replaceFirst("(.*)/[^/]*","$1"); fn=fileName.replaceFirst(".*/([^/]*)","$1"); } try { if (!(new File(dir)).exists()) (new File(dir)).mkdir(); DataOutputStream out=new DataOutputStream(new FileOutputStream(dir + "/" + fn+ ".log",true)); out.writeBytes(timestamp + " (" + dateTime+ ") ["+ session+ "]"+ un+ " ["+ type+ "] "+ text.replaceAll("\\n","~n")+ "\n"); out.flush(); out.close(); } catch ( IOException ex) { ex.printStackTrace(); } }
Example 19
From project addis, under directory /application/src/test/java/org/drugis/addis/.
Source file: ExampleData.java

private static Study realBuildStudyMcMurray(){ Study study=new Study("McMurray et al, 2003",buildIndicationChronicHeartFailure()); study.getEndpoints().clear(); study.getEndpoints().addAll(Study.wrapVariables(Collections.singletonList(buildEndpointCVdeath()))); study.setCharacteristic(BasicStudyCharacteristic.BLINDING,BasicStudyCharacteristic.Blinding.DOUBLE_BLIND); study.setCharacteristic(BasicStudyCharacteristic.CENTERS,618); study.setCharacteristic(BasicStudyCharacteristic.ALLOCATION,BasicStudyCharacteristic.Allocation.RANDOMIZED); study.setCharacteristic(BasicStudyCharacteristic.INCLUSION,"Eligible patients were aged 18 years or older, had left-" + "ventricular ejection fraction 40% or lower measured " + "within the past 6 months, New York Heart Association "+ "functional class II??V (if class II, patients had to have "+ "admission to hospital for a cardiac reason in the previous "+ "6 months), and treatment with an ACE inhibitor at a "+ "constant dose for 30 days or longer."); study.setCharacteristic(BasicStudyCharacteristic.EXCLUSION,""); study.setCharacteristic(BasicStudyCharacteristic.OBJECTIVE,"Angiotensin II type 1 receptor blockers have " + "favourable effects on heamodynamic measurements, " + "neurohumoral activity and left-ventricular remodelling when "+ "added to angiotensin-converting-enzyme (ACE) inhibitors in "+ "patients with chronic heart failure (CHF). We aimed to find "+ "out whether these drugs improve clinical outcome."); study.setCharacteristic(BasicStudyCharacteristic.STATUS,BasicStudyCharacteristic.Status.COMPLETED); Calendar startDate=Calendar.getInstance(); startDate.set(1999,Calendar.MARCH,1,0,0,0); study.setCharacteristic(BasicStudyCharacteristic.STUDY_START,startDate.getTime()); Calendar endDate=Calendar.getInstance(); endDate.set(2003,Calendar.MARCH,31,0,0,0); study.setCharacteristic(BasicStudyCharacteristic.STUDY_END,endDate.getTime()); addDefaultEpochs(study); FixedDose cDose=new FixedDose(32,DoseUnit.createMilliGramsPerDay()); Arm cand=study.createAndAddArm("Candesartan-0",1273,buildDrugCandesartan(),cDose); BasicRateMeasurement cDeath=new BasicRateMeasurement(302,cand.getSize()); FixedDose pDose=new FixedDose(32,DoseUnit.createMilliGramsPerDay()); Arm placebo=study.createAndAddArm("Placebo-1",1271,buildPlacebo(),pDose); BasicRateMeasurement pDeath=new BasicRateMeasurement(347,placebo.getSize()); addDefaultMeasurementMoments(study); study.setMeasurement(study.findStudyOutcomeMeasure(buildEndpointCVdeath()),cand,cDeath); study.setMeasurement(study.findStudyOutcomeMeasure(buildEndpointCVdeath()),placebo,pDeath); return study; }
Example 20
public Integer call(){ Thread.currentThread().setName("loader(" + id + ")"); ThreadVars.dateMaker.set(dateMaker); ThreadVars.valueFactory.set(conn.getValueFactory()); final int statusSize=Defaults.STATUS; int count=0, errors=0; Vector<Statement> statements=new Vector<Statement>(triplesPerCommit); Calendar start=GregorianCalendar.getInstance(), end; statements.setSize(triplesPerCommit); for (int loop=0; loop < loopCount; loop++) { for (int event=0, index=0; event < eventsPerCommit; event++) { index=AllEvents.makeEvent(statements,index); } if (loop > 0 && (loop % statusSize) == 0) { end=GregorianCalendar.getInstance(); double seconds=(end.getTimeInMillis() - start.getTimeInMillis()) / 1000.0; trace("Loading Status - %d triples loaded so " + "far at %d triples per commit (%.2f commits/sec, %.2f triples/sec" + " over last %d commits), %d errors.",count,triplesPerCommit,logtime(statusSize / seconds),logtime(statusSize * triplesPerCommit / seconds),statusSize,errors); start=end; } try { conn.add(statements); count+=triplesPerCommit; } catch ( Exception e) { errors++; trace("Error adding statements..."); e.printStackTrace(); } } trace("Loading Done - %d triples at %d triples " + "per commit, %d errors.",count,triplesPerCommit,errors); return 0; }
Example 21
From project activiti-webapp-rest-generic, under directory /activiti-webapp-rest/src/main/java/org/activiti/rest/util/.
Source file: JSONUtil.java

public static String formatISO8601Date(Calendar calendar){ if (calendar != null) { return ISO8601DateFormat.format(calendar.getTime()); } return null; }