Example usage for org.joda.time DateTime toLocalDate

List of usage examples for org.joda.time DateTime toLocalDate

Introduction

In this page you can find the example usage for org.joda.time DateTime toLocalDate.

Prototype

public LocalDate toLocalDate() 

Source Link

Document

Converts this object to a LocalDate with the same date and chronology.

Usage

From source file:com.axelor.data.adapter.JodaAdapter.java

License:Open Source License

@Override
public Object adapt(Object value, Map<String, Object> context) {

    if (value == null || !(value instanceof String)) {
        return value;
    }/* w  w w.  j  av a 2  s  .  c o  m*/

    String format = this.get("format", DEFAULT_FORMAT);

    DateTimeFormatter fmt = DateTimeFormat.forPattern(format);
    DateTime dt;
    try {
        dt = fmt.parseDateTime((String) value);
    } catch (Exception e) {
        throw new IllegalArgumentException("Invalid value: " + value, e);
    }

    String type = this.get("type", null);

    if ("LocalDate".equals(type)) {
        return dt.toLocalDate();
    }
    if ("LocalTime".equals(type)) {
        return dt.toLocalTime();
    }
    if ("LocalDateTime".equals(type)) {
        return dt.toLocalDateTime();
    }
    return dt;
}

From source file:com.divudi.bean.common.CommonController.java

public boolean sameDate(Date date1, Date date2) {
    Calendar d1 = Calendar.getInstance();
    d1.setTime(date1);/*w  ww  .ja v a 2 s.c om*/
    DateTime first = new DateTime(date1);
    DateTime second = new DateTime(date2);
    LocalDate firstDate = first.toLocalDate();
    LocalDate secondDate = second.toLocalDate();
    return firstDate.equals(secondDate);
}

From source file:com.gst.infrastructure.core.service.DateUtils.java

License:Apache License

public static LocalDate parseLocalDate(final String stringDate, final String pattern) {

    try {/* w  w w.  j  a  v a2s.c o  m*/
        final DateTimeFormatter dateStringFormat = DateTimeFormat.forPattern(pattern);
        dateStringFormat.withZone(getDateTimeZoneOfTenant());
        final DateTime dateTime = dateStringFormat.parseDateTime(stringDate);
        return dateTime.toLocalDate();
    } catch (final IllegalArgumentException e) {
        final List<ApiParameterError> dataValidationErrors = new ArrayList<>();
        final ApiParameterError error = ApiParameterError.parameterError("validation.msg.invalid.date.pattern",
                "The parameter date (" + stringDate + ") is invalid w.r.t. pattern " + pattern, "date",
                stringDate, pattern);
        dataValidationErrors.add(error);
        throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist",
                "Validation errors exist.", dataValidationErrors);
    }
}

From source file:com.gst.infrastructure.reportmailingjob.service.ReportMailingJobWritePlatformServiceImpl.java

License:Apache License

/**
 * create the next recurring DateTime from recurrence pattern, start DateTime and current DateTime
 * //from w  ww  .ja va2s. c o m
 * @param recurrencePattern
 * @param startDateTime
 * @return DateTime object
 */
private DateTime createNextRecurringDateTime(final String recurrencePattern, final DateTime startDateTime) {
    DateTime nextRecurringDateTime = null;

    // the recurrence pattern/rule cannot be empty
    if (StringUtils.isNotBlank(recurrencePattern) && startDateTime != null) {
        final LocalDate nextDayLocalDate = startDateTime.plus(1).toLocalDate();
        final LocalDate nextRecurringLocalDate = CalendarUtils.getNextRecurringDate(recurrencePattern,
                startDateTime.toLocalDate(), nextDayLocalDate);
        final String nextDateTimeString = nextRecurringLocalDate + " " + startDateTime.getHourOfDay() + ":"
                + startDateTime.getMinuteOfHour() + ":" + startDateTime.getSecondOfMinute();
        final DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(DATETIME_FORMAT);

        nextRecurringDateTime = DateTime.parse(nextDateTimeString, dateTimeFormatter);
    }

    return nextRecurringDateTime;
}

From source file:com.helger.datetime.format.PDTFromString.java

License:Apache License

@Nullable
public static LocalDate getLocalDateFromString(@Nullable final String sValue,
        @Nonnull final DateTimeFormatter aDF) {
    final DateTime aDT = getDateTimeFromString(sValue, aDF);
    return aDT == null ? null : aDT.toLocalDate();
}

From source file:com.helger.datetime.format.PDTFromString.java

License:Apache License

@Nullable
public static LocalDate getLocalDateFromString(@Nullable final String sValue, @Nonnull final String sPattern) {
    final DateTime aDT = getDateTimeFromString(sValue, sPattern);
    return aDT == null ? null : aDT.toLocalDate();
}

From source file:com.helger.datetime.PDTFactory.java

License:Apache License

@Nonnull
public static LocalDate createLocalDate(@Nonnull final DateTime aDateTime) {
    return aDateTime.toLocalDate();
}

From source file:com.helger.maven.buildinfo.GenerateBuildInfoMojo.java

License:Apache License

private Map<String, String> _determineBuildInfoProperties() {
    // Get the current time, using the time zone specified in the settings
    final DateTime aDT = PDTFactory.getCurrentDateTime();

    // Build the default properties
    final Map<String, String> aProps = new LinkedHashMap<String, String>();
    // Version 1: initial
    // Version 2: added dependency information; added per build plugin the key
    // property/*from  w ww  .  j a va  2  s. co m*/
    aProps.put("buildinfo.version", "2");

    // Project information
    aProps.put("project.groupid", project.getGroupId());
    aProps.put("project.artifactid", project.getArtifactId());
    aProps.put("project.version", project.getVersion());
    aProps.put("project.name", project.getName());
    aProps.put("project.packaging", project.getPackaging());

    // Parent project information (if available)
    final MavenProject aParentProject = project.getParent();
    if (aParentProject != null) {
        aProps.put("parentproject.groupid", aParentProject.getGroupId());
        aProps.put("parentproject.artifactid", aParentProject.getArtifactId());
        aProps.put("parentproject.version", aParentProject.getVersion());
        aProps.put("parentproject.name", aParentProject.getName());
    }

    // All reactor projects (nested projects)
    // Don't emit this, if this is "1" as than only the current project would be
    // listed
    if (reactorProjects != null && reactorProjects.size() != 1) {
        final String sPrefix = "reactorproject.";

        // The number of reactor projects
        aProps.put(sPrefix + "count", Integer.toString(reactorProjects.size()));

        // Show details of all reactor projects, index starting at 0
        int nIndex = 0;
        for (final MavenProject aReactorProject : reactorProjects) {
            aProps.put(sPrefix + nIndex + ".groupid", aReactorProject.getGroupId());
            aProps.put(sPrefix + nIndex + ".artifactid", aReactorProject.getArtifactId());
            aProps.put(sPrefix + nIndex + ".version", aReactorProject.getVersion());
            aProps.put(sPrefix + nIndex + ".name", aReactorProject.getName());
            ++nIndex;
        }
    }

    // Build Plugins
    final List<?> aBuildPlugins = project.getBuildPlugins();
    if (aBuildPlugins != null) {
        final String sPrefix = "build.plugin.";
        // The number of build plugins
        aProps.put(sPrefix + "count", Integer.toString(aBuildPlugins.size()));

        // Show details of all plugins, index starting at 0
        int nIndex = 0;
        for (final Object aObj : aBuildPlugins) {
            final Plugin aPlugin = (Plugin) aObj;
            aProps.put(sPrefix + nIndex + ".groupid", aPlugin.getGroupId());
            aProps.put(sPrefix + nIndex + ".artifactid", aPlugin.getArtifactId());
            aProps.put(sPrefix + nIndex + ".version", aPlugin.getVersion());
            final Object aConfiguration = aPlugin.getConfiguration();
            if (aConfiguration != null) {
                // Will emit an XML structure!
                aProps.put(sPrefix + nIndex + ".configuration", aConfiguration.toString());
            }
            aProps.put(sPrefix + nIndex + ".key", aPlugin.getKey());
            ++nIndex;
        }
    }

    // Build dependencies
    final List<?> aDependencies = project.getDependencies();
    if (aDependencies != null) {
        final String sDepPrefix = "dependency.";
        // The number of build plugins
        aProps.put(sDepPrefix + "count", Integer.toString(aDependencies.size()));

        // Show details of all dependencies, index starting at 0
        int nDepIndex = 0;
        for (final Object aDepObj : aDependencies) {
            final Dependency aDependency = (Dependency) aDepObj;
            aProps.put(sDepPrefix + nDepIndex + ".groupid", aDependency.getGroupId());
            aProps.put(sDepPrefix + nDepIndex + ".artifactid", aDependency.getArtifactId());
            aProps.put(sDepPrefix + nDepIndex + ".version", aDependency.getVersion());
            aProps.put(sDepPrefix + nDepIndex + ".type", aDependency.getType());
            if (aDependency.getClassifier() != null)
                aProps.put(sDepPrefix + nDepIndex + ".classifier", aDependency.getClassifier());
            aProps.put(sDepPrefix + nDepIndex + ".scope", aDependency.getScope());
            if (aDependency.getSystemPath() != null)
                aProps.put(sDepPrefix + nDepIndex + ".systempath", aDependency.getSystemPath());
            aProps.put(sDepPrefix + nDepIndex + ".optional", Boolean.toString(aDependency.isOptional()));
            aProps.put(sDepPrefix + nDepIndex + ".managementkey", aDependency.getManagementKey());

            // Add all exclusions of the current dependency
            final List<?> aExclusions = aDependency.getExclusions();
            if (aExclusions != null) {
                final String sExclusionPrefix = sDepPrefix + nDepIndex + ".exclusion.";
                // The number of build plugins
                aProps.put(sExclusionPrefix + "count", Integer.toString(aExclusions.size()));

                // Show details of all dependencies, index starting at 0
                int nExclusionIndex = 0;
                for (final Object aExclusionObj : aExclusions) {
                    final Exclusion aExclusion = (Exclusion) aExclusionObj;
                    aProps.put(sExclusionPrefix + nExclusionIndex + ".groupid", aExclusion.getGroupId());
                    aProps.put(sExclusionPrefix + nExclusionIndex + ".artifactid", aExclusion.getArtifactId());
                    ++nExclusionIndex;
                }
            }

            ++nDepIndex;
        }
    }

    // Build date and time
    aProps.put("build.datetime", aDT.toString());
    aProps.put("build.datetime.millis", Long.toString(aDT.getMillis()));
    aProps.put("build.datetime.date", aDT.toLocalDate().toString());
    aProps.put("build.datetime.time", aDT.toLocalTime().toString());
    aProps.put("build.datetime.timezone", aDT.getZone().getID());
    final int nOfsMilliSecs = aDT.getZone().getOffset(aDT);
    aProps.put("build.datetime.timezone.offsethours",
            Long.toString(nOfsMilliSecs / CGlobal.MILLISECONDS_PER_HOUR));
    aProps.put("build.datetime.timezone.offsetmins",
            Long.toString(nOfsMilliSecs / CGlobal.MILLISECONDS_PER_MINUTE));
    aProps.put("build.datetime.timezone.offsetsecs",
            Long.toString(nOfsMilliSecs / CGlobal.MILLISECONDS_PER_SECOND));
    aProps.put("build.datetime.timezone.offsetmillisecs", Integer.toString(nOfsMilliSecs));

    // Emit system properties?
    if (withAllSystemProperties || CollectionHelper.isNotEmpty(selectedSystemProperties))
        for (final Map.Entry<String, String> aEntry : CollectionHelper
                .getSortedByKey(SystemProperties.getAllProperties()).entrySet()) {
            final String sName = aEntry.getKey();
            if (withAllSystemProperties || _matches(selectedSystemProperties, sName))
                if (!_matches(ignoredSystemProperties, sName))
                    aProps.put("systemproperty." + sName, aEntry.getValue());
        }

    // Emit environment variable?
    if (withAllEnvVars || CollectionHelper.isNotEmpty(selectedEnvVars))
        for (final Map.Entry<String, String> aEntry : CollectionHelper.getSortedByKey(System.getenv())
                .entrySet()) {
            final String sName = aEntry.getKey();
            if (withAllEnvVars || _matches(selectedEnvVars, sName))
                if (!_matches(ignoredEnvVars, sName))
                    aProps.put("envvar." + sName, aEntry.getValue());
        }

    return aProps;
}

From source file:com.hmsinc.epicenter.util.DateTimeUtils.java

License:Open Source License

public static boolean isToday(final DateTime date) {
    return new LocalDate().equals(date.toLocalDate());
}

From source file:com.hmsinc.epicenter.util.DateTimeUtils.java

License:Open Source License

public static boolean isSameDay(final DateTime date, final DateTime otherDate) {
    return date.toLocalDate().equals(otherDate.toLocalDate());
}