Example usage for org.joda.time LocalDateTime fromCalendarFields

List of usage examples for org.joda.time LocalDateTime fromCalendarFields

Introduction

In this page you can find the example usage for org.joda.time LocalDateTime fromCalendarFields.

Prototype

public static LocalDateTime fromCalendarFields(Calendar calendar) 

Source Link

Document

Constructs a LocalDateTime from a java.util.Calendar using exactly the same field values.

Usage

From source file:at.struct.wasbug21.converter.JodaTimeValueHandler.java

License:Apache License

@Override
public Object toObjectValue(ValueMapping vm, Object val) {
    if (vm.getType() == LocalTime.class) {
        return LocalTime.fromDateFields((Date) val);
    } else if (vm.getType() == LocalDate.class) {
        return LocalDate.fromDateFields((Date) val);
    } else if (vm.getType() == LocalDateTime.class) {
        return LocalDateTime.fromCalendarFields((Calendar) val);
    }//from w w w .  j ava  2 s. com

    throw new IllegalStateException(
            "only LocalTime, LocalDate and LocalDateTime can be handled with this ValueHandler");
}

From source file:com.kccomy.orgar.ui.editor.NodeEditorFragment.java

License:Apache License

@Override
public void onDateSet(com.wdullaer.materialdatetimepicker.date.DatePickerDialog view, int year, int month,
        int day) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(year, month, day);//from  w  w  w  .  ja  v  a 2 s  .  c o  m
    LocalDateTime date = LocalDateTime.fromCalendarFields(calendar);

    String tag = view.getTag();

    if (OrgTimestamp.Type.SCHEDULED.toString().equals(tag)) {

        scheduledDateBtn.setText(date.toString(DATE_FORMAT));
        scheduledDate = date;

        if (scheduledTime != null) {
            scheduledDate.withTime(scheduledTime.getHourOfDay(), scheduledTime.getMinuteOfHour(), 0, 0);
        }

    } else if (OrgTimestamp.Type.DEADLINE.toString().equals(tag)) {

        deadlineDateBtn.setText(date.toString(DATE_FORMAT));
        deadlineDate = date;

        if (deadlineTime != null) {
            deadlineDate.withTime(deadlineTime.getHourOfDay(), deadlineTime.getMinuteOfHour(), 0, 0);
        }
    }

}

From source file:com.ramzcalender.RWeekCalendar.java

License:Open Source License

/**
 * Set set date of the selected week/*from   ww w.ja  v  a  2 s  .c  o  m*/
 *
 * @param calendar
 */
public void setDateWeek(Calendar calendar) {

    LocalDateTime ldt = LocalDateTime.fromCalendarFields(calendar);

    AppController.getInstance().setSelected(ldt);

    int nextPage = Weeks.weeksBetween(mStartDate, ldt).getWeeks();

    if (nextPage >= 0 && nextPage < getWeekBetweenDates(start, end)) {

        pager.setCurrentItem(nextPage);
        calenderListener.onSelectDate(ldt);
        WeekFragment fragment = (WeekFragment) pager.getAdapter().instantiateItem(pager, nextPage);
        fragment.ChangeSelector(ldt);
    }

}

From source file:com.weekcalendar.utils.CalUtil.java

License:Open Source License

/**
 * Initial calculation of the week//from www.jav  a2  s .co  m
 */
public void calculate(Context mContext) {

    //Initializing JodaTime
    JodaTimeAndroid.init(mContext);

    //Initializing Start with current month
    final LocalDateTime currentDateTime = new LocalDateTime();

    setStartDate(currentDateTime.getYear(), currentDateTime.getMonthOfYear(), currentDateTime.getDayOfMonth());

    int weekGap = CalUtil.mDateGap(currentDateTime.dayOfWeek().getAsText().substring(0, 3).toLowerCase());

    if (weekGap != 0) {
        //if the current date is not the first day of the week the rest of days is added

        //Calendar set to the current date
        Calendar calendar = Calendar.getInstance();

        calendar.add(Calendar.DAY_OF_YEAR, -weekGap);

        //now the date is weekGap days back
        LocalDateTime ldt = LocalDateTime.fromCalendarFields(calendar);

        setStartDate(ldt.getYear(), ldt.getMonthOfYear(), ldt.getDayOfMonth());

    }
}

From source file:com.weekcalendar.WeekCalendarFragment.java

License:Open Source License

/**
 * Set set date of the selected week/*from   w ww  . ja va 2  s .co m*/
 */
public void setDateWeek(Calendar calendar) {
    LocalDateTime ldt = LocalDateTime.fromCalendarFields(calendar);
    AppController.getInstance().setSelected(ldt);
    int nextPage = Weeks.weeksBetween(mStartDate, ldt).getWeeks();
    if (nextPage < 0 || (nextPage == 0 && ldt.isBefore(mStartDate))) {
        // make selected date to previous week on viewpager onSelected:
        // if result is negative or it is 0 weeks but earlier than week start date.
        --nextPage;
    }
    if (nextPage >= -mMiddlePoint && nextPage < mMiddlePoint) {
        mViewPager.setCurrentItem(nextPage + mMiddlePoint);
        if (mCalenderListener != null) {
            mCalenderListener.onSelectDate(ldt);
        }
        WeekFragment fragment = (WeekFragment) mViewPager.getAdapter().instantiateItem(mViewPager,
                nextPage + mMiddlePoint);
        fragment.ChangeSelector(ldt);
    }
}

From source file:griffon.plugins.jodatime.editors.LocalDateTimePropertyEditor.java

License:Apache License

protected void setValueInternal(Object value) {
    if (null == value) {
        super.setValueInternal(null);
    } else if (value instanceof LocalDateTime) {
        super.setValueInternal(value);
    } else if (value instanceof LocalDate) {
        super.setValueInternal(((LocalDate) value).toDateTimeAtStartOfDay());
    } else if (value instanceof LocalTime) {
        super.setValueInternal(((LocalTime) value).toDateTimeToday());
    } else if (value instanceof DateMidnight) {
        super.setValueInternal(((DateMidnight) value).toDateTime().toLocalDateTime());
    } else if (value instanceof CharSequence) {
        handleAsString(String.valueOf(value));
    } else if (value instanceof Calendar) {
        super.setValueInternal(LocalDateTime.fromCalendarFields((Calendar) value));
    } else if (value instanceof Date) {
        super.setValueInternal(LocalDateTime.fromDateFields((Date) value));
    } else if (value instanceof Number) {
        super.setValueInternal(new LocalDateTime(((Number) value).longValue()));
    } else {/* w  w w . j a v  a  2s .  c o  m*/
        throw illegalValue(value, LocalDateTime.class);
    }
}

From source file:org.awknet.commons.converter.DateTimeConverter.java

License:Open Source License

/**
 * Define a type to be formatted. Use DateTime instead of LocalDateTime to
 * match with requirements of joda-time-hibernate-1.2 - EXCEPTION: can't
 * convert LocalDateTime to DateTime.// w  w  w  .  j  av a  2s  .  c o  m
 * 
 * @param value
 *            to be converted.
 * @param type
 *            that can be [LocalTime | DateTime | LocalDate].
 * @param context
 *            VRaptor injection.
 * @return a DateTime converted.
 * @throws ConversionException
 */
public Object convert(String value, Class<?> type, LogicRequest context) throws ConversionException {
    DateTimeConverter.LOG
            .debug("+++++++++++++++++++++++++USING CUSTOM CONVERTER FOR DATETIME+++++++++++++++++++++++++");
    if (value == null || value.equals("")) {
        return null;
    }

    try {
        // OLD WAY! - only return a Date instead of a DateTime!!
        // return LocalDateTime.fromDateFields(getDateFormat(type,
        // context).parse(value)).toDateTime();
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(getDateFormat(type, context).parse(value));
        return LocalDateTime.fromCalendarFields(calendar).toDateTime();
    } catch (Exception e) {
        DateTimeConverter.LOG.error("Unable to parse string " + value + " --- Custom converter!!! ", e);
        throw new ConversionException("invalid_date",
                "Unable to parse string " + value + " --- Custom converter!!! ", e);
    }
}

From source file:org.cowboyprogrammer.org.OrgTimestamp.java

License:Open Source License

/**
 * @param millis   Milliseconds since the epoch.
 * @param withTime true if time part is considered valid.
 *//* w ww  . j  a v  a2  s  .  c  om*/
public OrgTimestamp(final long millis, final boolean withTime) {
    this();
    final Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(millis);
    date = LocalDateTime.fromCalendarFields(cal);
    hasTime = withTime;
}