Java Code Examples for org.joda.time.DateTimeComparator

The following are top voted examples for showing how to use org.joda.time.DateTimeComparator. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: OnOff-Tracker   File: CardItemAdapter.java   View source code 6 votes vote down vote up
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    ActionsInterval item = items.get(position);
    String title = item.title;
    if (DateTimeComparator.getDateOnlyInstance().compare(item.startDate, item.endDate) != 0) {
        title = String.format(holder.title.getContext().getString(R.string.cardview_title),
                item.title,
                fmt.print(item.startDate),
                fmt.print(item.endDate));

    }
    holder.title.setText(title);
    holder.screenOn.setText(String.format(holder.screenOn.getContext().getString(R.string.cardview_screenon), item.screenOn));
    holder.screenOff.setText(String.format(holder.screenOff.getContext().getString(R.string.cardview_screenoff), item.screenOff));
    holder.unlocked.setText(String.format(holder.unlocked.getContext().getString(R.string.cardview_unlocked), item.unlocked));
}
 
Example 2
Project: SpartaHack-Android   File: SchedulePresenterTest.java   View source code 6 votes vote down vote up
@Test public void testCompare() throws Exception {
    Event rhsEvent = mock(Event.class);
    Event lhsEvent = mock(Event.class);

    DateTimeComparator dateTimeComparator = DateTimeComparator.getInstance();

    DateTime rhsDateTime = new DateTime(2017, 1, 20, 0, 0, 0, 0);
    DateTime lhsDateTime = new DateTime(2017, 1, 21, 0, 0, 0, 0);

    // set each event to return their respective DateTime
    when(rhsEvent.getTime()).thenReturn(rhsDateTime);
    when(lhsEvent.getTime()).thenReturn(lhsDateTime);

    // comparison test on DateTimes where lhs > rhs
    assertEquals(dateTimeComparator.compare(lhsEvent.getTime(), rhsEvent.getTime()), presenter.compare(lhsEvent, rhsEvent));

    rhsDateTime = new DateTime(2017, 1, 21, 0, 0, 0, 0);
    lhsDateTime = new DateTime(2017, 1, 20, 0, 0, 0, 0);

    // set each event to return their respective DateTime
    when(rhsEvent.getTime()).thenReturn(rhsDateTime);
    when(lhsEvent.getTime()).thenReturn(lhsDateTime);

    // comparison test on DateTimes where lhs < rhs
    assertEquals(dateTimeComparator.compare(lhsEvent.getTime(), rhsEvent.getTime()), presenter.compare(lhsEvent, rhsEvent));
}
 
Example 3
Project: togg   File: DateTimeFormatter.java   View source code 6 votes vote down vote up
public static String dayOrDate(Date date) {
    int actualDayOfYear = Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
    int actualYear = Calendar.getInstance().get(Calendar.YEAR);
    Calendar c = Calendar.getInstance();
    c.setTime(date);

    DateTimeComparator comparator = DateTimeComparator
            .getDateOnlyInstance();

    String result;
    if (comparator.compare(date, new Date()) == 0) {
        result = ToggApp.getApplication().getResources()
                .getString(R.string.today);
    } else if (actualYear == c.get(Calendar.YEAR)
            && actualDayOfYear - c.get(Calendar.DAY_OF_YEAR) == 1) {
        result = ToggApp.getApplication().getResources()
                .getString(R.string.yesterday);
    } else {
        result = literalDate(date);
    }

    return result;
}
 
Example 4
Project: wulkanowy   File: TimetableFragmentTab.java   View source code 5 votes vote down vote up
private boolean getExpanded(String dayDate) {
    DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(DATE_PATTERN);
    DateTime dayTime = dateTimeFormatter.parseDateTime(dayDate);

    DateTime currentDate = new DateTime();

    if (currentDate.getDayOfWeek() == DateTimeConstants.SATURDAY) {
        currentDate = currentDate.plusDays(2);
    } else if (currentDate.getDayOfWeek() == DateTimeConstants.SUNDAY) {
        currentDate = currentDate.plusDays(1);
    }

    return DateTimeComparator.getDateOnlyInstance().compare(currentDate, dayTime) == 0;
}
 
Example 5
Project: cassandra-reaper   File: RepairRun.java   View source code 5 votes vote down vote up
/**
 * Order RepairRun instances by time. Primarily endTime, secondarily startTime. Descending, i.e. latest first.
 *
 * @param other the RepairRun compared to
 * @return negative if this RepairRun is later than the specified RepairRun. Positive if earlier. 0 if equal.
 */
@Override
public int compareTo(RepairRun other) {
  DateTimeComparator comparator = DateTimeComparator.getInstance();
  int endTimeComparison = comparator.compare(endTime, other.endTime);
  if (endTimeComparison != 0) {
    return -endTimeComparison;
  } else {
    return -comparator.compare(startTime, other.startTime);
  }
}
 
Example 6
Project: dhis2-android-eventcapture   File: FormSectionActivity.java   View source code 5 votes vote down vote up
private void showDatePickerDialog() {
    final OnDateSetListener onDateSetListener = new OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.YEAR, year);
            calendar.set(Calendar.MONTH, monthOfYear);
            calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

            String stringDate = (new SimpleDateFormat(DATE_FORMAT, Locale.US))
                    .format(calendar.getTime());
            String newValue = String.format(Locale.getDefault(), "%s: %s",
                    getString(R.string.report_date), stringDate);
            textViewReportDate.setText(newValue);

            DateTime currentDateTime = DateTime.now();
            DateTime selectedDateTime = DateTime.parse(stringDate);

            /*
            * in case when user selected today's date, we need to know about time as well.
            * selectedDateTime does not contain time information (only date), that's why we
            * need to create a new DateTime object by calling DateTime.now()
            */
            DateTime dateTime;
            if (DateTimeComparator.getDateOnlyInstance()
                    .compare(currentDateTime, selectedDateTime) == 0) {
                dateTime = currentDateTime;
            } else {
                dateTime = selectedDateTime;
            }

            formSectionPresenter.saveEventDate(getEventUid(), dateTime);
        }
    };

    DatePickerDialogFragment datePickerDialogFragment =
            DatePickerDialogFragment.newInstance(false);
    datePickerDialogFragment.setOnDateSetListener(onDateSetListener);
    datePickerDialogFragment.show(getSupportFragmentManager());
}
 
Example 7
Project: sakai   File: EventSummary.java   View source code 5 votes vote down vote up
private boolean isToday(long dateMs) {
	TimeZone timeZone = getCurrentUserTimezone();
	DateTime thisDate = new DateTime(dateMs).withZone(DateTimeZone.forTimeZone(timeZone));
	//Start of day at this local
	DateTime today = new DateTime().withTime(0, 0, 0, 0).withZone(DateTimeZone.forTimeZone(timeZone));
	DateTimeComparator dtComp = DateTimeComparator.getDateOnlyInstance();
	return  (dtComp.compare(thisDate, today) == 0);
	
}
 
Example 8
Project: message-server   File: MMXTopicsItemsResourceTest.java   View source code 5 votes vote down vote up
private void sortAscending(List<MMXPubSubItem> list) {
  Collections.sort(list, new Comparator<MMXPubSubItem>(){
    public int compare(MMXPubSubItem o1, MMXPubSubItem o2){
      MMXPubSubPayload payload1 = o1.getPayload();
      MMXPubSubPayload payload2 = o2.getPayload();
      DateTime d1 = new DateTime(payload1.getCreationDate());
      DateTime d2 = new DateTime(payload2.getCreationDate());
      return DateTimeComparator.getInstance().compare(d1, d2);
    }
  });
}
 
Example 9
Project: message-server   File: MMXTopicsItemsResourceTest.java   View source code 5 votes vote down vote up
private void sortDescending(List<MMXPubSubItem> list) {
  Collections.sort(list, new Comparator<MMXPubSubItem>(){
    public int compare(MMXPubSubItem o1, MMXPubSubItem o2){
      MMXPubSubPayload payload1 = o1.getPayload();
      MMXPubSubPayload payload2 = o2.getPayload();
      DateTime d1 = new DateTime(payload1.getCreationDate());
      DateTime d2 = new DateTime(payload2.getCreationDate());
      return DateTimeComparator.getInstance().compare(d2, d1);
    }
  });
}
 
Example 10
Project: SpartaHack-Android   File: AnnouncementPresenter.java   View source code 5 votes vote down vote up
@Override public int compare(Announcement lhs, Announcement rhs) {
    if (!rhs.getPinned() && !lhs.getPinned())
        return DateTimeComparator.getInstance().compare(rhs.getCreatedAt(), lhs.getCreatedAt());
    else if (rhs.getPinned() && lhs.getPinned())
        return DateTimeComparator.getInstance().compare(rhs.getCreatedAt(), lhs.getCreatedAt());
    else if (rhs.getPinned())
        return RIGHT_FIRST;
    return LEFT_FIRST;
}
 
Example 11
Project: SpartaHack-Android   File: ScheduleFragment.java   View source code 5 votes vote down vote up
@Override public void showEvents(ArrayList<Event> events) {
    EventListAdapter eventListAdapter = new EventListAdapter(getActivity(), events);

    ArrayList<SimpleSectionedRecyclerViewAdapter.Section> sections = new ArrayList<>();

    DateTime date = events.get(0).getTime();
    SimpleDateFormat formatDate = new SimpleDateFormat("EEEE", Locale.US);

    sections.add(new SimpleSectionedRecyclerViewAdapter.Section(0, formatDate.format(date.toDate())));
    // location the header should go at
    int sectionLoc = 0;
    for (Event e : events){
        if (DateTimeComparator.getDateOnlyInstance().compare(e.getTime(), date) == 1){
            sections.add(new SimpleSectionedRecyclerViewAdapter.Section(sectionLoc, formatDate.format(e.getTime().toDate())));
            date = e.getTime();
        }
        sectionLoc++;
    }

    SimpleSectionedRecyclerViewAdapter.Section[] dummy = new SimpleSectionedRecyclerViewAdapter.Section[sections.size()];

    SimpleSectionedRecyclerViewAdapter adapter = new SimpleSectionedRecyclerViewAdapter(getActivity(), R.layout.section_header, R.id.section_text, eventListAdapter);

    adapter.setSections(sections.toArray(dummy));

    recyclerView.setAdapter(adapter);

    errorMessage.setVisibility(View.GONE);
    progressBar.setVisibility(View.GONE);
    recyclerView.setVisibility(View.VISIBLE);
}
 
Example 12
Project: togg   File: TimeEntriesAdapter.java   View source code 5 votes vote down vote up
private DateTimeComparator getComparator() {
    if (mComparator == null) {
        this.mComparator = DateTimeComparator.getDateOnlyInstance();
    }

    return mComparator;
}
 
Example 13
Project: sakai   File: EventSummary.java   View source code 5 votes vote down vote up
private boolean isToday(long dateMs) {
	TimeZone timeZone = getCurrentUserTimezone();
	DateTime thisDate = new DateTime(dateMs).withZone(DateTimeZone.forTimeZone(timeZone));
	//Start of day at this local
	DateTime today = new DateTime().withTime(0, 0, 0, 0).withZone(DateTimeZone.forTimeZone(timeZone));
	DateTimeComparator dtComp = DateTimeComparator.getDateOnlyInstance();
	return  (dtComp.compare(thisDate, today) == 0);
	
}
 
Example 14
Project: BlogReader-javafx   File: DateUtilTest.java   View source code 5 votes vote down vote up
/**
 * Test of parseDateRfc822 method, of class DateUtil.
 */
@Test
public void testParseDateRfc822() {
    String s = "Wed, 02 Oct 2002 15:00:00 +0200";
    DateTime expResult = DateUtil.createDate(
            2002, DateTimeConstants.OCTOBER, 2, 
            15, 0, 0, 0,
            "+02:00");
    DateTime result = DateUtil.parseDateRfc822(s);
    assertNotNull(result);
    if (result != null) {
        assertEquals(expResult.getMillis(), result.getMillis());
    }
    //assertEquals(expResult, result);
    //assertTrue(expResult.equals(result));
    assertEquals(0, DateTimeComparator.getInstance().compare(expResult, result));
    
    s = "Tue, 24 Feb 2009 08:30:36 +0000";
    expResult = DateUtil.createDate(
            2009, DateTimeConstants.FEBRUARY, 24, 
            8, 30, 36, 0,
            "+00:00");
    result = DateUtil.parseDateRfc822(s);
    assertNotNull(result);
    if (result != null) {
        assertEquals(expResult.getMillis(), result.getMillis());
    }
    //assertEquals(expResult, result);
    //assertTrue(expResult.equals(result));
    assertEquals(0, DateTimeComparator.getInstance().compare(expResult, result));
}
 
Example 15
Project: atlas-deer   File: BroadcastAggregator.java   View source code 5 votes vote down vote up
Collection<ResolvedBroadcast> sortBroadcastsByDateTime(ImmutableMultimap<DateTime, ResolvedBroadcast> broadcastMap) {

        List<DateTime> dateTimes = Lists.newArrayList(broadcastMap.keySet());
        dateTimes.sort(DateTimeComparator.getInstance());

        Ordering<ResolvedBroadcast> dateTimeOrdering = Ordering.explicit(dateTimes)
                .onResultOf(resolvedBroadcast ->
                        resolvedBroadcast.getBroadcast().getTransmissionTime()
                );

        return dateTimeOrdering.sortedCopy(broadcastMap.values());
    }
 
Example 16
Project: Weather-Guru-MVP   File: DateUtils.java   View source code 4 votes vote down vote up
/**
 * Check if some date is the current day
 */
private static boolean isToday(String date) {
  return DateTimeComparator.getDateOnlyInstance().compare(new DateTime(), new DateTime(date))
      == 0;
}
 
Example 17
Project: messagemedia-rest-api-java-sdk   File: JsonUtilitiesTest.java   View source code 4 votes vote down vote up
@Test
@UseDataProvider("validDateStrings")
public void testDeserializationOfValidDateString(String dateString, DateTime dateObject) throws ParseException, IOException {
    DateTimeComparator dateTimeComparator = DateTimeComparator.getInstance();
    assertEquals(0, dateTimeComparator.compare(dateObject, bytesToObject(("\"" + dateString + "\"").getBytes("UTF-8"), DateTime.class)));
}
 
Example 18
Project: Portofino   File: PositionComparator.java   View source code 4 votes vote down vote up
public PositionComparator() {
    dateTimeComparator = DateTimeComparator.getInstance();
    stringComparator = new StringComparator();
}
 
Example 19
Project: Portofino   File: EventWeekComparator.java   View source code 4 votes vote down vote up
public EventWeekComparator() {
    dateTimeComparator = DateTimeComparator.getInstance();
    stringComparator = new StringComparator();
}
 
Example 20
Project: civilizer   File: FragmentOrder.java   View source code 4 votes vote down vote up
@Override
public int compare(Fragment arg0, Fragment arg1) {
    DateTimeComparator comparator = DateTimeComparator.getInstance();
    return comparator.compare(arg0.getUpdateDatetime(), arg1.getUpdateDatetime());
}
 
Example 21
Project: civilizer   File: FragmentOrder.java   View source code 4 votes vote down vote up
@Override
public int compare(Fragment arg0, Fragment arg1) {
    DateTimeComparator comparator = DateTimeComparator.getInstance();
    return comparator.compare(arg0.getCreationDatetime(), arg1.getCreationDatetime());
}
 
Example 22
Project: SpartaHack-Android   File: SchedulePresenter.java   View source code 4 votes vote down vote up
@Override public int compare(Event lhs, Event rhs) {
    return DateTimeComparator.getInstance().compare(lhs.getTime(), rhs.getTime());
}
 
Example 23
Project: manydesigns.cn   File: EventWeekComparator.java   View source code 4 votes vote down vote up
public EventWeekComparator() {
    dateTimeComparator = DateTimeComparator.getInstance();
    stringComparator = new StringComparator();
}
 
Example 24
Project: BlogReader-javafx   File: Controller.java   View source code 4 votes vote down vote up
/**
 * 
 * @param url
 * @param rb 
 */
@Override
public void initialize(URL url, ResourceBundle rb) {

    logger.log(Level.INFO, "initialize");

    // initialize label
    titleLabel.textProperty().bind(model.titleProperty());

    progressIndicator.visibleProperty().bind(isLoading);

    // initialize table
    // * add two columns
    // * bind the model
    // * add selection listener
    TableColumn titleColumn = new TableColumn();
    titleColumn.setText(TITLE_COLUMN_TEXT);
    titleColumn.prefWidthProperty().bind(itemsTableView.widthProperty().subtract(200));
    titleColumn.setCellValueFactory(new PropertyValueFactory<FeedItem, String>(TITLE_PROPERTY_NAME));

    TableColumn pubDateColumn = new TableColumn();
    pubDateColumn.setText(DATE_COLUMN_TEXT);
    pubDateColumn.setPrefWidth(180);
    pubDateColumn.setCellValueFactory(new PropertyValueFactory<FeedItem, DateTime>(PUBDATE_PROPERTY_NAME));
    pubDateColumn.setCellFactory(getDateTimeCellFactory(DATE_FORMAT));
    pubDateColumn.setComparator(DateTimeComparator.getInstance());

    itemsTableView.getColumns().add(titleColumn);
    itemsTableView.getColumns().add(pubDateColumn);
    itemsTableView.itemsProperty().bind(model.itemsProperty());
    itemsTableView.getSelectionModel().selectedItemProperty()
            .addListener(new SelectionChangeListener(itemWebView));

    // initialize webview
    // * disable the contextmenu
    // * listen to statechanges
    itemWebView.setContextMenuEnabled(false);
    final WebEngine webEngine = itemWebView.getEngine();
    webEngine.getLoadWorker().stateProperty().addListener(
            new WebEngineChangeListener(webEngine));

    // fetch data in the background
    logger.log(Level.INFO, "Begin loading feed");
    Task<Document> getFeedDocument = new LoadDocumentTask(FEED_URL);
    getFeedDocument.valueProperty().addListener(new ChangeListener<Document>() {
        @Override
        public void changed(ObservableValue<? extends Document> ov, Document t, Document t1) {
            model.itemsProperty().clear();
            Document doc = t1;
            if (doc != null) {
                final String title = FeedParser.readTitle(doc);
                final List<FeedItem> items = FeedParser.readItems(doc);
                if (title != null && items != null) {
                    logger.log(Level.INFO, "Feed loaded and read");
                    model.titleProperty().set(title);
                    model.itemsProperty().addAll(items);
                    itemsTableView.getSelectionModel().selectFirst();
                } else {
                    logger.log(Level.SEVERE, "Error reading feed");
                    model.titleProperty().set("Error reading feed");
                }
            } else {
                logger.log(Level.SEVERE, "Error loading feed");
                model.titleProperty().set("Error loading feed");
            }
            isLoading.set(false);
        }
    });
    new Thread(getFeedDocument).start();

}
 
Example 25
Project: kafka-connect-mixpanel   File: DateUtils.java   View source code 3 votes vote down vote up
public static int compare(String d1, String d2){

        DateTime date1 = DateTime.parse(d1);
        DateTime date2 = DateTime.parse(d2);

        return DateTimeComparator.getDateOnlyInstance().compare(date1, date2);
    }