List of usage examples for org.joda.time LocalDateTime toLocalTime
public LocalTime toLocalTime()
From source file:de.avanux.smartapplianceenabler.appliance.DayTimeframe.java
License:Open Source License
protected Interval buildMidnightAdjustedInterval(LocalDateTime now) { if (start != null && end != null) { LocalDateTime earliestStartDateTime = new LocalDate(now).toLocalDateTime(start.toLocalTime()); LocalDateTime latestEndDateTime = new LocalDate(now).toLocalDateTime(end.toLocalTime()); if (isOverMidnight(earliestStartDateTime, latestEndDateTime)) { if (now.toLocalTime().isAfter(start.toLocalTime())) { // before midnight latestEndDateTime = latestEndDateTime.plusHours(24); } else if (now.toLocalTime().isBefore(end.toLocalTime())) { // after midnight, before end earliestStartDateTime = earliestStartDateTime.minusHours(24); } else { // after midnight, after end latestEndDateTime = latestEndDateTime.plusHours(24); }//from ww w .j ava 2 s .co m } return new Interval(earliestStartDateTime.toDateTime(), latestEndDateTime.toDateTime()) .withChronology(ISOChronology.getInstance()); } return null; }
From source file:de.avanux.smartapplianceenabler.appliance.DayTimeframeCondition.java
License:Open Source License
/** * Returns true, if the given time is contained by the interval identified by start and end. * @param now/*from www . java2 s . c om*/ * @return */ public boolean isMet(LocalDateTime now) { if (start.toLocalTime().isBefore(now.toLocalTime()) && end.toLocalTime().isAfter(now.toLocalTime())) { return true; } return false; }
From source file:ee.ut.soras.ajavtV2.mudel.ajavaljend.arvutus.TimeMLDateTimePoint.java
License:Open Source License
public TimeMLDateTimePoint(LocalDateTime lct) { this.underlyingTime = lct.toLocalTime(); this.underlyingDate = lct.toLocalDate(); inputFields.put(VALUE_FIELD.YEAR,/*from w w w . j a va 2s. co m*/ normalizeByAddingZeroes(VALUE_FIELD.YEAR, (this.underlyingDate).getYear())); inputFields.put(VALUE_FIELD.MONTH_OR_WEEK, normalizeByAddingZeroes(VALUE_FIELD.MONTH_OR_WEEK, (this.underlyingDate).getMonthOfYear())); inputFields.put(VALUE_FIELD.DAY, normalizeByAddingZeroes(VALUE_FIELD.DAY, (this.underlyingDate).getDayOfMonth())); inputFields.put(VALUE_FIELD.HOUR_OR_POD, normalizeByAddingZeroes(VALUE_FIELD.HOUR_OR_POD, (this.underlyingTime).getHourOfDay())); inputFields.put(VALUE_FIELD.MINUTE, normalizeByAddingZeroes(VALUE_FIELD.MINUTE, (this.underlyingTime).getMinuteOfHour())); }
From source file:ee.ut.soras.ajavtV2.mudel.ajavaljend.arvutus.TimeMLDateTimePoint.java
License:Open Source License
public void addToField(Granulaarsus field, int value) { if (field == Granulaarsus.MINUTE || field == Granulaarsus.HOUR_OF_HALF_DAY || field == Granulaarsus.HOUR_OF_DAY) { // Put date and time together LocalDateTime ajaFookus = getAsLocalDateTime(); // perform operation if (field == Granulaarsus.MINUTE) { ajaFookus = ajaFookus.plusMinutes(value); this.underlyingTime = ajaFookus.toLocalTime(); this.underlyingDate = ajaFookus.toLocalDate(); if (dateModified) { updateDateRepresentation(Granulaarsus.DAY_OF_MONTH, null, false, ADD_TYPE_OPERATION); }//from w ww. j a v a2s . co m updateTimeRepresentation(field, null, false, ADD_TYPE_OPERATION); functionOtherThanSetUsed = true; } if (field == Granulaarsus.HOUR_OF_DAY || field == Granulaarsus.HOUR_OF_HALF_DAY) { ajaFookus = ajaFookus.plusHours(value); this.underlyingTime = ajaFookus.toLocalTime(); this.underlyingDate = ajaFookus.toLocalDate(); if (dateModified) { updateDateRepresentation(Granulaarsus.DAY_OF_MONTH, null, false, ADD_TYPE_OPERATION); } updateTimeRepresentation(field, null, false, ADD_TYPE_OPERATION); functionOtherThanSetUsed = true; } } if (field == Granulaarsus.DAY_OF_MONTH) { this.underlyingDate = (this.underlyingDate).plusDays(value); updateDateRepresentation(field, null, false, ADD_TYPE_OPERATION); functionOtherThanSetUsed = true; dateModified = true; } if (field == Granulaarsus.DAY_OF_WEEK) { this.underlyingDate = (this.underlyingDate).plusDays(value); updateDateRepresentation(field, null, false, ADD_TYPE_OPERATION); functionOtherThanSetUsed = true; dateModified = true; } if (field == Granulaarsus.WEEK_OF_YEAR) { this.underlyingDate = (this.underlyingDate).plusWeeks(value); updateDateRepresentation(field, null, false, ADD_TYPE_OPERATION); functionOtherThanSetUsed = true; dateModified = true; } if (field == Granulaarsus.MONTH) { this.underlyingDate = (this.underlyingDate).plusMonths(value); updateDateRepresentation(field, null, false, ADD_TYPE_OPERATION); functionOtherThanSetUsed = true; dateModified = true; } if (field == Granulaarsus.YEAR) { this.underlyingDate = (this.underlyingDate).plusYears(value); updateDateRepresentation(field, null, false, ADD_TYPE_OPERATION); functionOtherThanSetUsed = true; dateModified = true; } if (field == Granulaarsus.YEAR_OF_CENTURY) { // NB! Toimib nagu tavalise aastaarvu muutmine this.underlyingDate = (this.underlyingDate).plusYears(value); updateDateRepresentation(field, null, false, ADD_TYPE_OPERATION); functionOtherThanSetUsed = true; dateModified = true; } if (field == Granulaarsus.CENTURY_OF_ERA) { this.underlyingDate = (this.underlyingDate).plusYears(value * 100); updateDateRepresentation(field, null, false, ADD_TYPE_OPERATION); functionOtherThanSetUsed = true; dateModified = true; } }
From source file:ee.ut.soras.ajavtV2.mudel.ajavaljend.arvutus.TimeMLDateTimePoint.java
License:Open Source License
public void addToField(Granulaarsus field, Period period, int direction) { if (field.compareByCoarseRank(Granulaarsus.WEEK_OF_YEAR) == -1) { // ---- paev, tund, minut LocalDateTime ajaFookus = getAsLocalDateTime(); try {//from w w w . ja va2s .c o m if (direction > 0) { ajaFookus = ajaFookus.plus(period); } else { ajaFookus = ajaFookus.minus(period); } this.functionOtherThanSetUsed = true; } catch (Exception e) { // Erindi korral jaabki muutmata } this.underlyingTime = ajaFookus.toLocalTime(); this.underlyingDate = ajaFookus.toLocalDate(); if (field.compareByCoarseRank(Granulaarsus.DAY_OF_MONTH) == 0) { this.dateModified = true; updateTimeRepresentation(Granulaarsus.HOUR_OF_DAY, null, false, ADD_TYPE_OPERATION); } else { updateTimeRepresentation(Granulaarsus.MINUTE, null, false, ADD_TYPE_OPERATION); } if (dateModified) { updateDateRepresentation(Granulaarsus.DAY_OF_MONTH, null, false, ADD_TYPE_OPERATION); } } else { // ---- nadal, kuu, aasta, sajand try { if (direction > 0) { this.underlyingDate = (this.underlyingDate).plus(period); } else { this.underlyingDate = (this.underlyingDate).minus(period); } this.functionOtherThanSetUsed = true; this.dateModified = true; } catch (Exception e) { // Erindi korral jaabki muutmata } if (field == Granulaarsus.MONTH || field == Granulaarsus.WEEK_OF_YEAR) { updateDateRepresentation(Granulaarsus.DAY_OF_MONTH, null, false, ADD_TYPE_OPERATION); } if (field == Granulaarsus.YEAR) { updateDateRepresentation(Granulaarsus.MONTH, null, false, ADD_TYPE_OPERATION); } if (field == Granulaarsus.CENTURY_OF_ERA) { updateDateRepresentation(Granulaarsus.YEAR, null, false, ADD_TYPE_OPERATION); } } }
From source file:ee.ut.soras.ajavtV2.mudel.ajavaljend.arvutus.TimeMLDateTimePoint.java
License:Open Source License
public void seekField(Granulaarsus field, int direction, String soughtValue, boolean excludingCurrent) { // --------------------------------- // PART OF DAY // --------------------------------- if (field == Granulaarsus.TIME && soughtValue != null && soughtValue.matches("(NI|MO|AF|EV)") && direction != 0) { int dir = (direction > 0) ? (1) : (-1); // Loome k2esolevat ajafookust t2ielikult kirjeldava objekti LocalDateTime ajaFookus = getAsLocalDateTime(); // AlgneMargend ehk p2evaosa, millest peame ennem m66da saama, kuni v6ime uue // v22rtuse omaks v6tta String algneMargend = (excludingCurrent) ? (SemLeidmiseAbimeetodid.getPartOfDay(ajaFookus.toLocalTime())) : (null);//from ww w . j av a 2 s . c o m if (!excludingCurrent) { // V6tame sammu tagasi, et esimene nihe tooks meid t2pselt k2esolevale tunnile ajaFookus = ajaFookus.plusHours(dir * (-1)); } int count = 0; while (true) { LocalDateTime uusFookus = ajaFookus.plusHours(1 * dir); ajaFookus = uusFookus; String newPartOfDay = SemLeidmiseAbimeetodid.getPartOfDay(uusFookus.toLocalTime()); if (algneMargend != null) { if (algneMargend.equals(newPartOfDay)) { continue; } else { algneMargend = null; } } if (newPartOfDay != null && newPartOfDay.equals(soughtValue)) { algneMargend = newPartOfDay; count++; if (count == Math.abs(direction)) { break; } } } this.partOfDay = soughtValue; updateTimeRepresentation(Granulaarsus.TIME, soughtValue, true, ADD_TYPE_OPERATION); this.underlyingTime = ajaFookus.toLocalTime(); this.underlyingDate = ajaFookus.toLocalDate(); functionOtherThanSetUsed = true; } // --------------------------------- // WORKDAY OR WEEKEND // --------------------------------- if (field == Granulaarsus.DAY_OF_WEEK && soughtValue != null && soughtValue.matches("(WD|WE)") && direction != 0) { int dir = (direction > 0) ? (1) : (-1); LocalDate ajaFookus = new LocalDate(this.underlyingDate); // Algne p2ev ehk p2ev, millest tahame tingimata m66duda String algneMargend = (excludingCurrent) ? (SemLeidmiseAbimeetodid.getWordDayOrWeekend(ajaFookus)) : (null); if (!excludingCurrent) { // V6tame sammu tagasi, et esimene nihe tooks meid t2pselt k2esolevale p2evale ajaFookus = ajaFookus.plusDays(dir * (-1)); } int count = 0; while (true) { LocalDate nihutatudFookus = ajaFookus.plusDays(1 * dir); ajaFookus = nihutatudFookus; String toopaevVoiNadalalopp = SemLeidmiseAbimeetodid.getWordDayOrWeekend(ajaFookus); if (algneMargend != null) { if (algneMargend.equals(toopaevVoiNadalalopp)) { continue; } else { algneMargend = null; } } if (toopaevVoiNadalalopp.equals(soughtValue)) { algneMargend = toopaevVoiNadalalopp; count++; if (count == Math.abs(direction)) { break; } } } this.underlyingDate = ajaFookus; (this.openedFields).put(VALUE_FIELD.DAY, soughtValue); updateDateRepresentation(Granulaarsus.DAY_OF_WEEK, soughtValue, true, ADD_TYPE_OPERATION); functionOtherThanSetUsed = true; this.dateModified = true; } // --------------------------------- // SEASONs // --------------------------------- if (field == Granulaarsus.MONTH && soughtValue != null && soughtValue.matches("(SP|SU|FA|WI)") && direction != 0) { int dir = (direction > 0) ? (1) : (-1); LocalDate ajaFookus = new LocalDate(this.underlyingDate); // Algne aastaaeg ehk aastaaeg, millest tahame tingimata m66duda String algneMargend = (excludingCurrent) ? (SemLeidmiseAbimeetodid.getSeason(ajaFookus)) : (null); if (!excludingCurrent) { // V6tame sammu tagasi, et esimene nihe tooks meid t2pselt k2esolevale kuule ajaFookus = ajaFookus.plusMonths(dir * (-1)); } int count = 0; while (true) { LocalDate nihutatudFookus = ajaFookus.plusMonths(1 * dir); ajaFookus = nihutatudFookus; String aastaaeg = SemLeidmiseAbimeetodid.getSeason(ajaFookus); if (algneMargend != null) { if (algneMargend.equals(aastaaeg)) { continue; } else { algneMargend = null; } } if (aastaaeg.equals(soughtValue)) { algneMargend = aastaaeg; count++; if (count == Math.abs(direction)) { break; } } } this.underlyingDate = ajaFookus; // Detsembri puhul liigume j2rgmisesse aastasse (st - talve loetakse aasta algusest) ... if ((this.underlyingDate).getMonthOfYear() == DateTimeConstants.DECEMBER) { this.underlyingDate = (this.underlyingDate).plusMonths(1); } openedFields.put(VALUE_FIELD.MONTH_OR_WEEK, soughtValue); updateDateRepresentation(Granulaarsus.MONTH, soughtValue, true, ADD_TYPE_OPERATION); functionOtherThanSetUsed = true; this.dateModified = true; } // --------------------------------- // QUARTERs // --------------------------------- if (field == Granulaarsus.MONTH && soughtValue != null && soughtValue.matches("Q(1|2|3|4)") && direction != 0) { int dir = (direction > 0) ? (1) : (-1); LocalDate ajaFookus = new LocalDate(this.underlyingDate); // Algne kvartal ehk kvartal, millest tahame tingimata m66duda String algneMargend = (excludingCurrent) ? (SemLeidmiseAbimeetodid.getQuarterOfYear(ajaFookus)) : (null); if (!excludingCurrent) { // V6tame sammu tagasi, et esimene nihe tooks meid t2pselt k2esolevale kuule ajaFookus = ajaFookus.plusMonths(dir * (-1)); } int count = 0; while (true) { LocalDate nihutatudFookus = ajaFookus.plusMonths(1 * dir); ajaFookus = nihutatudFookus; String kvartal = SemLeidmiseAbimeetodid.getQuarterOfYear(ajaFookus); if (algneMargend != null) { if (algneMargend.equals(kvartal)) { continue; } else { algneMargend = null; } } if (kvartal.equals(soughtValue)) { algneMargend = kvartal; count++; if (count == Math.abs(direction)) { break; } } } this.underlyingDate = ajaFookus; String newQuarterVal = SemLeidmiseAbimeetodid.getQuarterOfYear(this.underlyingDate); openedFields.put(VALUE_FIELD.MONTH_OR_WEEK, newQuarterVal); updateDateRepresentation(Granulaarsus.MONTH, newQuarterVal, true, ADD_TYPE_OPERATION); functionOtherThanSetUsed = true; this.dateModified = true; } }
From source file:ee.ut.soras.ajavtV2.mudel.ajavaljend.arvutus.TimeMLDateTimePoint.java
License:Open Source License
/** * Applies the algorithm of Baldwin's window (as implemented in {@link SemLeidmiseAbimeetodid}), * in order to find closest moment of time within a Baldwin's window, which satisfies the * constraint <tt>field = soughtValue</tt>. * <br>/*from ww w . j ava 2s . c o m*/ * <br> * Only limited number of granularities are supported currently. */ public void applyBalwinWindow(Granulaarsus field, String soughtValue) { if (soughtValue != null) { LocalDateTime ajaFookus = getAsLocalDateTime(); try { int soughtValueAsInt = Integer.parseInt(soughtValue); // Only some date fields currently supported LocalDateTime uusFookus = SemLeidmiseAbimeetodid.applyBaldwinWindow(field, ajaFookus, soughtValueAsInt); this.underlyingTime = uusFookus.toLocalTime(); this.underlyingDate = uusFookus.toLocalDate(); if (field == Granulaarsus.DAY_OF_WEEK) { // Force the usage of month, in order to get canonical form in output field = Granulaarsus.DAY_OF_MONTH; } updateDateRepresentation(field, null, false, ADD_TYPE_OPERATION); functionOtherThanSetUsed = true; this.dateModified = true; } catch (NumberFormatException e) { LocalDateTime uusFookus = SemLeidmiseAbimeetodid.applyBaldwinWindow(field, ajaFookus, soughtValue); if (uusFookus != null) { // NB! Currently only season labels and quarter labels are supported if (field == Granulaarsus.MONTH) { this.underlyingTime = uusFookus.toLocalTime(); this.underlyingDate = uusFookus.toLocalDate(); openedFields.put(VALUE_FIELD.MONTH_OR_WEEK, soughtValue); updateDateRepresentation(Granulaarsus.MONTH, soughtValue, true, ADD_TYPE_OPERATION); functionOtherThanSetUsed = true; this.dateModified = true; } } } } }
From source file:ee.ut.soras.ajavtV2.mudel.ajavaljend.arvutus.TimeMLDateTimePoint.java
License:Open Source License
/** * Applies method {@link SemLeidmiseAbimeetodid#findNthSubpartOfGranularity(Granulaarsus, Granulaarsus, int, int, LocalDateTime)}}. *//*from ww w . ja v a 2 s .c om*/ public void findNthSubpartOfGranularity(Granulaarsus superField, Granulaarsus subField, int soughtValueOfSubField, int n) { LocalDateTime ajaFookus = getAsLocalDateTime(); LocalDateTime uusFookus = SemLeidmiseAbimeetodid.findNthSubpartOfGranularity(superField, subField, soughtValueOfSubField, n, ajaFookus); if (uusFookus != null) { if (superField == Granulaarsus.MONTH) { this.underlyingTime = uusFookus.toLocalTime(); this.underlyingDate = uusFookus.toLocalDate(); if (subField == Granulaarsus.DAY_OF_WEEK) { if (!(this.maskedFields).containsKey(VALUE_FIELD.MONTH_OR_WEEK)) { // open month and dayOfMonth, otherwise fields will remain closed manageMaskedFields(Granulaarsus.MONTH, null, false, SET_TYPE_OPERATION); } updateDateRepresentation(Granulaarsus.DAY_OF_MONTH, null, false, ADD_TYPE_OPERATION); functionOtherThanSetUsed = true; this.dateModified = true; } else if (subField == Granulaarsus.WEEK_OF_YEAR) { if (!(this.maskedFields).containsKey(VALUE_FIELD.MONTH_OR_WEEK)) { // open month and dayOfMonth, otherwise fields will remain closed manageMaskedFields(Granulaarsus.MONTH, null, false, SET_TYPE_OPERATION); } updateDateRepresentation(Granulaarsus.WEEK_OF_YEAR, null, false, ADD_TYPE_OPERATION); functionOtherThanSetUsed = true; this.dateModified = true; } } } }
From source file:fi.riista.integration.lupahallinta.HarvestReportExportCSVDTO.java
public static HarvestReportExportCSVDTO create(final Harvest harvest, final MessageSource messageSource) { HarvestReportExportCSVDTO dto = new HarvestReportExportCSVDTO(); dto.submissionRowCount = 1;/*from w ww . j ava 2 s . co m*/ dto.reportingTime = DateUtil.toLocalDateTimeNullSafe(harvest.getCreationTime()); final Person hunter = harvest.getActualShooter(); if (hunter != null) { dto.hunterName = hunter.getByName() + " " + hunter.getLastName(); dto.hunterFirstName = hunter.getByName(); dto.hunterLastName = hunter.getLastName(); Address address = hunter.getAddress(); if (address != null) { dto.hunterAddress = address.getStreetAddress(); dto.hunterPostalCode = address.getPostalCode(); dto.hunterPostalResidence = address.getCity(); } dto.hunterPhone = hunter.getPhoneNumber(); dto.hunterEmail = hunter.getEmail(); dto.hunterHuntingCard = hunter.getHunterNumber(); } dto.huntingLicenseNumber = getPermitNumberOrNull(harvest); dto.huntingLicenseAsList = getPermitReportsAsList(harvest.getHarvestPermit()); readSubmitter(harvest.getHarvestReportAuthor(), dto); if (harvest.getRhy() != null) { harvest.getRhy().getClosestAncestorOfType(OrganisationType.RKA).ifPresent(rka -> { dto.rkkAreaId = rka.getOfficialCode(); dto.rkkAreaName = rka.getNameFinnish(); }); dto.rhyAreaId = harvest.getRhy().getOfficialCode(); dto.rhyAreaName = harvest.getRhy().getNameFinnish(); } dto.coordinatesCollectionMethod = harvest.getGeoLocation().getSource(); dto.coordinatesLatitude = harvest.getGeoLocation().getLatitude(); dto.coordinatesLongitude = harvest.getGeoLocation().getLongitude(); dto.coordinatesAccuracy = harvest.getGeoLocation().getAccuracy() != null ? harvest.getGeoLocation().getAccuracy() : 0; dto.huntingArea = harvest.getHuntingAreaType(); dto.huntingGroup = harvest.getHuntingParty(); if (harvest.getHuntingAreaSize() != null) { dto.area = Math.round(harvest.getHuntingAreaSize()); } PropertyIdentifier propertyIdentifier = harvest.getPropertyIdentifier(); if (propertyIdentifier != null) { dto.municipality = propertyIdentifier.getKuntanumero(); dto.village = propertyIdentifier.getSijaintialuenumero(); dto.property = propertyIdentifier.getRyhmanumero(); dto.registerNumber = propertyIdentifier.getYksikkonumero(); } LocalDateTime huntingTimestamp = DateUtil.toLocalDateTimeNullSafe(harvest.getPointOfTime()); dto.dateOfCatch = huntingTimestamp.toLocalDate(); dto.timeOfCatch = huntingTimestamp.toLocalTime(); dto.animalId = harvest.getSpecies().getOfficialCode(); dto.animalSpecies = harvest.getSpecies().getNameFinnish(); if (harvest.getHuntingMethod() == HuntingMethod.SHOT) { dto.sealInformation = "Ammuttu"; } else if (harvest.getHuntingMethod() == HuntingMethod.SHOT_BUT_LOST) { dto.sealInformation = "Ammuttu, mutta menetetty"; } else if (harvest.getHuntingMethod() == HuntingMethod.CAPTURED_ALIVE) { dto.sealInformation = "Elvn pyytvll loukulla pyydetty"; } dto.amount = harvest.getAmount(); dto.genderId = transformSpecimens(harvest, genderIdTransformer()); dto.genderName = transformSpecimens(harvest, genderNameTransformer(messageSource)); dto.ageId = transformSpecimens(harvest, ageIdTransformer()); dto.ageName = transformSpecimens(harvest, ageNameTransformer(messageSource)); dto.weight = transformSpecimens(harvest, weightTransformer()); dto.harvestAlsoReportedByPhone = harvest.getReportedWithPhoneCall(); final Tuple2<String, String> p = createPermittedMethodPair(harvest.getPermittedMethod()); dto.permittedMethod = p._1(); dto.permittedMethodDescription = p._2(); return dto; }
From source file:fyp.hms.controller.AppointmentController.java
@RequestMapping(value = "/createApptment", params = "createApp", method = RequestMethod.GET) public String createApp(@RequestParam Map<String, String> parameters, ModelMap model, HttpSession session) throws ParseException { String customerId = (String) parameters.get("customerId"); String time = parameters.get("appointmentTime"); String apptDate = parameters.get("appointmentDate"); String hairStylistName = parameters.get("hairStylist"); String remarks = parameters.get("remarks"); String customerName = parameters.get("customerName"); String dob = parameters.get("dob"); String email = parameters.get("email"); String hairStylist = parameters.get("hairStylist"); String duration = parameters.get("duration"); //check empty fields (ps: remarks is allowed to be empty) if (parameters.get("customerId").length() == 0 || parameters.get("hairStylist").length() == 0 || parameters.get("appointmentDate").length() == 0 || parameters.get("customerName").length() == 0 || parameters.get("appointmentDate") == null || parameters.get("appointmentTime").length() == 0 || parameters.get("appointmentTime") == null || parameters.get("appointmentTime").equals("") || parameters.get("duration").length() == 0) { model.addAttribute("error", "Field(s) cannot be empty (except for remarks)."); model.addAttribute("customerId", customerId); model.addAttribute("time", time); model.addAttribute("apptDate", apptDate); model.addAttribute("hairStylistName", hairStylistName); model.addAttribute("remarks", remarks); model.addAttribute("customerName", customerName); model.addAttribute("dob", dob); model.addAttribute("email", email); model.addAttribute("hairStylist", hairStylist); model.addAttribute("duration", duration); return "addAppointment"; }//from w w w. j ava2 s . c o m //check the format of customer id int phoneNo = 0; try { phoneNo = Integer.parseInt(customerId); if (customerId.length() < 8) { model.addAttribute("error", "Phone number must contain at least 8 digits."); model.addAttribute("customerId", customerId); model.addAttribute("time", time); model.addAttribute("apptDate", apptDate); model.addAttribute("hairStylistName", hairStylistName); model.addAttribute("remarks", remarks); model.addAttribute("customerName", customerName); model.addAttribute("dob", dob); model.addAttribute("email", email); model.addAttribute("hairStylist", hairStylist); model.addAttribute("duration", duration); return "addAppointment"; } } catch (NumberFormatException e) { model.addAttribute("error", "Invalid Customer Id"); model.addAttribute("customerId", customerId); model.addAttribute("time", time); model.addAttribute("apptDate", apptDate); model.addAttribute("hairStylistName", hairStylistName); model.addAttribute("remarks", remarks); model.addAttribute("customerName", customerName); model.addAttribute("dob", dob); model.addAttribute("email", email); model.addAttribute("hairStylist", hairStylist); model.addAttribute("duration", duration); return "addAppointment"; } if (apptDate.length() != 10) { model.addAttribute("error", "Invalid appointment date."); model.addAttribute("customerId", customerId); model.addAttribute("time", time); model.addAttribute("apptDate", apptDate); model.addAttribute("hairStylistName", hairStylistName); model.addAttribute("remarks", remarks); model.addAttribute("customerName", customerName); model.addAttribute("dob", dob); model.addAttribute("email", email); model.addAttribute("hairStylist", hairStylist); model.addAttribute("duration", duration); return "addAppointment"; } //retrieve customer based on customer id Customer cust = appointmentService.retrieveCust(phoneNo); if (cust == null) { model.addAttribute("error", "Customer Id doesn't exist."); model.addAttribute("customerId", customerId); model.addAttribute("time", time); model.addAttribute("apptDate", apptDate); model.addAttribute("hairStylistName", hairStylistName); model.addAttribute("remarks", remarks); model.addAttribute("customerName", customerName); model.addAttribute("dob", dob); model.addAttribute("email", email); model.addAttribute("hairStylist", hairStylist); model.addAttribute("duration", duration); return "addAppointment"; } //convert util.date format to sql.date format SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date appointmentDate; java.sql.Date sqlDate = null; String strSqlDate = ""; try { appointmentDate = format.parse(apptDate); sqlDate = new java.sql.Date(appointmentDate.getTime()); strSqlDate = sqlDate + ""; } catch (ParseException ex) { model.addAttribute("error", "Invalid appointment date."); model.addAttribute("customerId", customerId); model.addAttribute("time", time); model.addAttribute("apptDate", apptDate); model.addAttribute("hairStylistName", hairStylistName); model.addAttribute("remarks", remarks); model.addAttribute("customerName", customerName); model.addAttribute("dob", dob); model.addAttribute("email", email); model.addAttribute("hairStylist", hairStylist); model.addAttribute("duration", duration); return "addAppointment"; } //check if the appointment date is before the current date Date currentDate = new Date(); java.sql.Date sqlCurrentDate = new java.sql.Date(currentDate.getTime()); String strSqlCurrentDate = sqlCurrentDate + ""; if (!strSqlDate.equals(strSqlCurrentDate) && sqlDate.before(sqlCurrentDate)) { model.addAttribute("error", "Appointment Date needs to be after current date."); model.addAttribute("customerId", customerId); model.addAttribute("time", time); model.addAttribute("apptDate", apptDate); model.addAttribute("hairStylistName", hairStylistName); model.addAttribute("remarks", remarks); model.addAttribute("customerName", customerName); model.addAttribute("dob", dob); model.addAttribute("email", email); model.addAttribute("hairStylist", hairStylist); model.addAttribute("duration", duration); return "addAppointment"; } //appointment time SimpleDateFormat timeDateFormat = new SimpleDateFormat("hh:mm"); long ms = timeDateFormat.parse(time).getTime(); int intDuration = Integer.parseInt(duration); long endMs = ms + (intDuration * 60000); Time t = new Time(ms); Time endTime = new Time(endMs); //get current time Calendar cal = Calendar.getInstance(); LocalDateTime localDateTime = LocalDateTime.now(); String localTime = localDateTime.toLocalTime() + ""; String strLocalTime = localTime.substring(0, 8); //String strCurrentTime = timeDateFormat.format(cal.getTime()); long currentMS = timeDateFormat.parse(strLocalTime).getTime(); Time currentTime = new Time(currentMS); //if appointment date is same as the current date if (strSqlDate.equals(strSqlCurrentDate)) { ////check if the appointment time is before the current time if (t.before(currentTime)) { model.addAttribute("error", "Appointment Time needs to be after current time."); model.addAttribute("customerId", customerId); model.addAttribute("time", time); model.addAttribute("apptDate", apptDate); model.addAttribute("hairStylistName", hairStylistName); model.addAttribute("remarks", remarks); model.addAttribute("customerName", customerName); model.addAttribute("dob", dob); model.addAttribute("email", email); model.addAttribute("hairStylist", hairStylist); model.addAttribute("duration", duration); return "addAppointment"; } } List<Appointment> apptList = appointmentService.retrieveAppointment(hairStylistName); /* if(apptList != null){ for(Appointment appt: apptList){ String strDate = appt.getAppointmentDate() + ""; if(strSqlDate.equals(strDate)){ Time apptTime = appt.getAppointmentTime(); Time apptEndTime = appt.getApptEndTime(); if(t.after(apptTime) && t.before(apptEndTime)){ model.addAttribute("error","The hairStylist has an appointment at this timing. Plese select another time."); model.addAttribute("customerId",customerId); model.addAttribute("time",time); model.addAttribute("apptDate",apptDate); model.addAttribute("hairStylistName",hairStylistName); model.addAttribute("remarks",remarks); model.addAttribute("customerName",customerName); model.addAttribute("dob",dob); model.addAttribute("email",email); model.addAttribute("hairStylist",hairStylist); model.addAttribute("duration",duration); return "addAppointment"; } String strAfterTime = t + ""; SimpleDateFormat dfAfter = new SimpleDateFormat("HH:mm"); Date afterDate = dfAfter.parse(strAfterTime); Calendar afterCal = Calendar.getInstance(); afterCal.setTime(afterDate); afterCal.add(Calendar.MINUTE, 60); String newAfterTime = dfAfter.format(afterCal.getTime()); long afterMS = dfAfter.parse(newAfterTime).getTime(); Time afterTime = new Time(afterMS); String strBeforeTime = t + ""; SimpleDateFormat dfBefore = new SimpleDateFormat("HH:mm"); Date beforeDate = dfBefore.parse(strBeforeTime); Calendar beforeCal = Calendar.getInstance(); beforeCal.setTime(beforeDate); beforeCal.add(Calendar.MINUTE, -60); String newBeforeTime = dfBefore.format(beforeCal.getTime()); long beforeMS = dfBefore.parse(newBeforeTime).getTime(); Time beforeTime = new Time(beforeMS); //Date beforeTime1 = beforeCal.getTime(); if(apptTime.after(beforeTime) && apptTime.before(afterTime)){ model.addAttribute("error","The hairStylist has an another appointment within 1 hour of this timing. Plese select another time."); model.addAttribute("customerId",customerId); model.addAttribute("time",time); model.addAttribute("apptDate",apptDate); model.addAttribute("hairStylistName",hairStylistName); model.addAttribute("remarks",remarks); model.addAttribute("customerName",customerName); model.addAttribute("dob",dob); model.addAttribute("email",email); model.addAttribute("hairStylist",hairStylist); model.addAttribute("duration",duration); return "addAppointment"; } } } }*/ boolean isAdded = appointmentService.addAppointment(phoneNo, hairStylistName, customerName, sqlDate, t, endTime, remarks, "Ongoing"); if (!isAdded) { model.addAttribute("error", "Appointment cannot be added."); } else { model.addAttribute("success", "Appointment has been added successfully."); } String username = (String) session.getAttribute("username"); String role = (String) session.getAttribute("role"); if (username != null && role != null) { if (role.equals("staff")) { apptList = appointmentService.retrieveAppointment(username); model.addAttribute("apptList", apptList); } else { apptList = appointmentService.retrieveAllAppointment(); model.addAttribute("apptList", apptList); } } return "appointment"; }