Example usage for org.joda.time DateTime year

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

Introduction

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

Prototype

public Property year() 

Source Link

Document

Get the year property which provides access to advanced functionality.

Usage

From source file:org.joda.example.time.DateTimePerformance.java

License:Apache License

private void checkJISOSetGetYear() {
    int COUNT = COUNT_FAST;
    // Is it fair to use only MutableDateTime here? You decide.
    //        MutableDateTime dt = new MutableDateTime();
    //        for (int i = 0; i < AVERAGE; i++) {
    //            start("JISO", "setGetYear");
    //            for (int j = 0; j < COUNT; j++) {
    //                dt.setYear(1972);
    //                int val = dt.getYear();
    //                if (val < 0) {System.out.println("Anti optimise");}
    //            }
    //            end(COUNT);
    //        }//from w  ww .j  av a 2 s  .  c o  m
    DateTime dt = new DateTime();
    for (int i = 0; i < AVERAGE; i++) {
        start("JISO", "setGetYear");
        for (int j = 0; j < COUNT; j++) {
            dt = dt.year().setCopy(1972);
            int val = dt.getYear();
            if (val < 0) {
                System.out.println("Anti optimise");
            }
        }
        end(COUNT);
    }
}

From source file:org.shadowmask.core.mask.rules.generalizer.impl.TimestampGeneralizer.java

License:Apache License

@Override
public Long generalize(Long timestamp, int hierarchyLevel) {
    if (timestamp == null) {
        return null;
    }// w w w  .ja  va  2s.  c o m

    if (hierarchyLevel > ROOT_HIERARCHY_LEVEL || hierarchyLevel < 0) {
        throw new MaskRuntimeException("Root hierarchy level of MobileGeneralizer is " + ROOT_HIERARCHY_LEVEL
                + ", invalid input hierarchy level[" + hierarchyLevel + "]");
    }

    if (hierarchyLevel == 0) {
        return timestamp;
    }

    try {
        DateTime dateTime = new DateTime(timestamp);
        switch (hierarchyLevel) {
        // mask ms.
        case 1:
            dateTime = new DateTime(dateTime.getYear(), dateTime.getMonthOfYear(), dateTime.getDayOfMonth(),
                    dateTime.getHourOfDay(), dateTime.getMinuteOfHour(), dateTime.getSecondOfMinute());
            break;
        // mask second.
        case 2:
            dateTime = new DateTime(dateTime.getYear(), dateTime.getMonthOfYear(), dateTime.getDayOfMonth(),
                    dateTime.getHourOfDay(), dateTime.getMinuteOfHour());
            break;
        // mask minute.
        case 3:
            dateTime = new DateTime(dateTime.getYear(), dateTime.getMonthOfYear(), dateTime.getDayOfMonth(),
                    dateTime.getHourOfDay(), dateTime.getMinuteOfHour());
            dateTime = dateTime.minuteOfHour().setCopy(0);
            break;
        // mask hour.
        case 4:
            dateTime = new DateTime(dateTime.getYear(), dateTime.getMonthOfYear(), dateTime.getDayOfMonth(),
                    dateTime.getHourOfDay(), dateTime.getMinuteOfHour());
            dateTime = dateTime.minuteOfHour().setCopy(0);
            dateTime = dateTime.hourOfDay().setCopy(0);
            break;
        // mask day.
        case 5:
            dateTime = new DateTime(dateTime.getYear(), dateTime.getMonthOfYear(), dateTime.getDayOfMonth(),
                    dateTime.getHourOfDay(), dateTime.getMinuteOfHour());
            dateTime = dateTime.minuteOfHour().setCopy(0);
            dateTime = dateTime.hourOfDay().setCopy(0);
            dateTime = dateTime.dayOfMonth().setCopy(1);
            break;
        // mask month.
        case 6:
            dateTime = new DateTime(dateTime.getYear(), dateTime.getMonthOfYear(), dateTime.getDayOfMonth(),
                    dateTime.getHourOfDay(), dateTime.getMinuteOfHour());
            dateTime = dateTime.minuteOfHour().setCopy(0);
            dateTime = dateTime.hourOfDay().setCopy(0);
            dateTime = dateTime.dayOfMonth().setCopy(1);
            dateTime = dateTime.monthOfYear().setCopy(1);
            break;
        // mask year.
        case 7:
            dateTime = new DateTime(dateTime.getYear(), dateTime.getMonthOfYear(), dateTime.getDayOfMonth(),
                    dateTime.getHourOfDay(), dateTime.getMinuteOfHour());
            dateTime = dateTime.minuteOfHour().setCopy(0);
            dateTime = dateTime.hourOfDay().setCopy(0);
            dateTime = dateTime.dayOfMonth().setCopy(1);
            dateTime = dateTime.monthOfYear().setCopy(1);
            dateTime = dateTime.year().setCopy(1901);
            break;
        }

        return dateTime.getMillis();
    } catch (Throwable e) {
        throw new MaskRuntimeException("Invalid timestamp to generalize:" + timestamp, e);
    }
}

From source file:org.slc.sli.bulk.extract.date.EntityDateHelper.java

License:Apache License

public static boolean isPastOrCurrentDate(String entityDate, DateTime upToDate, String type) {
    DateTime finalUpToDate = (upToDate == null) ? DateTime.now() : upToDate;

    if (YEAR_BASED_ENTITIES.contains(type)) {
        return isBeforeOrEqualYear(entityDate, finalUpToDate.year().get());
    } else {/*from   w  w w.ja  va2 s .  c  om*/
        return isBeforeOrEqualDate(entityDate, finalUpToDate);
    }
}

From source file:org.trakhound.www.trakhound.device_details.DeviceStatus.java

License:Open Source License

public static DeviceStatus get(UserConfiguration userConfig, String uniqueId) {

    if (userConfig != null) {

        DateTime now = DateTime.now();
        DateTime from = new DateTime(now.year().get(), now.monthOfYear().get(), now.dayOfMonth().get(), 0, 0,
                0);//w  ww . ja  va  2s . c o  m
        DateTime to = from.plusDays(1);

        DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
        String fromStr = fmt.print(from);
        String toStr = fmt.print(to);

        String urlSuffix = "data/get/?" + "token=" + userConfig.sessionToken + "&sender_id="
                + UserManagement.getSenderId() + "&devices=[{\"unique_id\":\"" + uniqueId + "\"}]" + "&from="
                + fromStr + "&to=" + toStr + "&command=" + "01111"; // Get Status, Controller, Oee, and Timers tables

        String url = Uri.withAppendedPath(ApiConfiguration.apiHost, urlSuffix).toString();

        String response = Requests.get(url);
        if (response != null && response.length() > 0) {

            try {

                JSONArray a = new JSONArray(response);

                if (a.length() > 0) {

                    JSONObject obj = a.getJSONObject(0);

                    DeviceStatus deviceStatus = new DeviceStatus();

                    deviceStatus.uniqueId = obj.getString("unique_id");

                    deviceStatus.statusInfo = StatusInfo.parse(obj.getJSONObject("status"));

                    deviceStatus.controllerInfo = ControllerInfo.parse(obj.getJSONObject("controller"));

                    deviceStatus.oeeInfo = OeeInfo.parse(obj.getJSONObject("oee"));

                    deviceStatus.timersInfo = TimersInfo.parse(obj.getJSONObject("timers"));

                    return deviceStatus;
                }
            } catch (JSONException ex) {
                Log.d("Exception", ex.getMessage());
            }
        }
    }

    return null;
}

From source file:org.trakhound.www.trakhound.device_list.DeviceStatus.java

License:Open Source License

public static DeviceStatus[] get(UserConfiguration userConfig) {

    if (userConfig != null) {

        try {/*w ww. j  a v a  2  s .  c o m*/

            DateTime now = DateTime.now();
            DateTime from = new DateTime(now.year().get(), now.monthOfYear().get(), now.dayOfMonth().get(), 0,
                    0, 0);
            DateTime to = from.plusDays(1);

            DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
            String fromStr = fmt.print(from);
            String toStr = fmt.print(to);

            String urlSuffix = "data/get/?" + "token=" + URLEncoder.encode(userConfig.sessionToken, "UTF-8")
                    + "&sender_id=" + URLEncoder.encode(UserManagement.getSenderId(), "UTF-8") + "&from="
                    + fromStr + "&to=" + toStr + "&command=0101"; // Get Status and Oee tables

            String url = Uri.withAppendedPath(ApiConfiguration.apiHost, urlSuffix).toString();

            String response = Requests.get(url);
            if (response != null && response.length() > 0) {

                ArrayList<DeviceStatus> devicesStatuses = new ArrayList<>();

                try {

                    JSONArray a = new JSONArray(response);

                    for (int i = 0; i < a.length(); i++) {

                        JSONObject obj = a.getJSONObject(i);

                        DeviceStatus deviceStatus = new DeviceStatus();

                        deviceStatus.uniqueId = obj.getString("unique_id");

                        deviceStatus.statusInfo = StatusInfo.parse(obj.getJSONObject("status"));

                        deviceStatus.oeeInfo = OeeInfo.parse(obj.getJSONObject("oee"));

                        devicesStatuses.add(deviceStatus);
                    }
                } catch (JSONException ex) {
                    Log.d("Exception", ex.getMessage());
                }

                DeviceStatus[] deviceStatusArray = new DeviceStatus[devicesStatuses.size()];
                return devicesStatuses.toArray(deviceStatusArray);
            }

        } catch (UnsupportedEncodingException ex) {
            Log.d("Exception", ex.getMessage());
        }
    }

    return null;
}

From source file:playground.johannes.gsv.synPop.invermo.SplitPlanTask.java

License:Open Source License

private void splitPlan(ProxyPlan plan, List<ProxyPlan> newPlans) {
    ProxyPlan subPlan = new ProxyPlan();

    DateTime prev = getDate(plan.getLegs().get(0));
    if (prev == null) {
        logger.warn("Cannot split plan. Neither start nor end time specified.");
        return;//from  ww  w  .jav  a  2 s.  c  o m
    }

    for (int i = 0; i < plan.getLegs().size(); i++) {
        ProxyObject leg = plan.getLegs().get(i);
        ProxyObject act = plan.getActivities().get(i);

        DateTime current = getDate(leg);
        if (current == null) {
            logger.warn("Cannot split plan. Neither start nor end time specified.");
            return;
        }

        int currentDays = current.dayOfYear().get() + (365 * current.year().get());
        int prevDays = prev.dayOfYear().get() + (365 * prev.year().get());
        int nights = currentDays - prevDays;

        //         if (current.dayOfYear().get() != prev.dayOfYear().get()) {
        if (nights > 0) {
            subPlan.setAttribute(MIDKeys.JOURNEY_DAYS, String.valueOf(nights + 1));

            subPlan.addActivity(act.clone());
            newPlans.add(subPlan);

            subPlan = new ProxyPlan();
            subPlan.addActivity(act.clone());
            subPlan.addLeg(leg.clone());
        } else {
            subPlan.setAttribute(MIDKeys.JOURNEY_DAYS, String.valueOf(nights + 1));

            subPlan.addActivity(act.clone());
            subPlan.addLeg(leg.clone());
        }

        prev = current;
    }

    int size = plan.getActivities().size();
    subPlan.addActivity(plan.getActivities().get(size - 1).clone());
    newPlans.add(subPlan);
}

From source file:syncthing.android.ui.session.DeviceCard.java

License:Open Source License

@BindingAdapter("deviceLastSeenText")
public static void deviceLastSeenText(TextView view, DateTime lastSeen) {
    if (lastSeen == null) {
        view.setText(R.string.unknown);// w w w. j  a v  a 2  s  .  co m
    } else {
        if (lastSeen.year().equals(epoch.year())) {
            view.setText(R.string.never);
        } else {
            view.setText(lastSeen.toString("yyyy-MM-dd HH:mm"));
        }
    }
}

From source file:test.sql.CustomerDataManager.java

License:Apache License

public String makeSubject() {
    // String sub = LINKAGE_SUBJECT_PREFIX + StressTester.randomString(8);

    DateTime now = new DateTime();
    int year, month, day, hour, minute, second;
    year = now.year().get();
    month = now.monthOfYear().get();//from  w  ww. j  av  a  2 s  .  c o m
    day = now.dayOfMonth().get();
    hour = now.getHourOfDay();
    minute = now.getMinuteOfHour();
    second = now.getSecondOfMinute();

    StringBuilder sb = new StringBuilder();
    sb.append(LINKAGE_SUBJECT_PREFIX);
    sb.append(StressTester.randomString(8));
    sb.append("_");
    sb.append(Integer.toString(year));
    if (month < 10)
        sb.append("0");
    sb.append(Integer.toString(month));
    if (day < 10)
        sb.append("0");
    sb.append(Integer.toString(day));
    sb.append("_");
    if (hour < 10)
        sb.append("0");
    sb.append(Integer.toString(hour));
    if (minute < 10)
        sb.append("0");
    sb.append(Integer.toString(minute));
    if (second < 10)
        sb.append("0");
    sb.append(Integer.toString(second));

    return sb.toString();
}

From source file:test.sql.TrackingDataManager.java

License:Apache License

public String makeSubject() {
    String sub = LINKAGE_SUBJECT_PREFIX + StressTester.randomString(8);

    DateTime now = new DateTime();
    int year, month, day, hour, minute, second;
    year = now.year().get();
    month = now.monthOfYear().get();//from w  w  w  .ja v a 2s.  co  m
    day = now.dayOfMonth().get();
    hour = now.getHourOfDay();
    minute = now.getMinuteOfHour();
    second = now.getSecondOfMinute();

    StringBuilder sb = new StringBuilder();
    sb.append(LINKAGE_SUBJECT_PREFIX);
    sb.append(StressTester.randomString(8));
    sb.append("_");
    sb.append(Integer.toString(year));
    if (month < 10)
        sb.append("0");
    sb.append(Integer.toString(month));
    if (day < 10)
        sb.append("0");
    sb.append(Integer.toString(day));
    sb.append("_");
    if (hour < 10)
        sb.append("0");
    sb.append(Integer.toString(hour));
    if (minute < 10)
        sb.append("0");
    sb.append(Integer.toString(minute));
    if (second < 10)
        sb.append("0");
    sb.append(Integer.toString(second));

    return sb.toString();
}

From source file:test.utilities.SemStoreLeakConverter.java

License:Apache License

void processLine(String line, PrintStream ps) throws Exception {

    if (line == null)
        return;/*from  www . j a  v a  2 s.c om*/

    StringTokenizer tokens = new StringTokenizer(line);
    if (tokens.countTokens() == 0)
        return;

    String dstr = tokens.nextToken();
    String tstr = tokens.nextToken();

    String datestr = dstr + "T" + tstr;

    DateTime dtime = new DateTime(datestr);

    // peel off Info:
    String info = tokens.nextToken();
    // peel off [Event
    String event = tokens.nextToken();
    // grab hold of operation with trailing ]
    String operation = tokens.nextToken();
    int bracket = operation.indexOf(']');
    if (bracket > 0)
        operation = operation.substring(0, bracket);

    // type of TripleStore in hand
    String storeType = tokens.nextToken();

    // the operation again
    String op = tokens.nextToken();

    // the address
    String addrStr = tokens.nextToken();
    int eq = addrStr.indexOf('=');
    if (eq > 0)
        addrStr = addrStr.substring(eq + 1);

    String memsize = null;
    // if it's a created line, we might have a trailing , and a mem size out there
    if (op.equals("created:")) {
        int comma = addrStr.indexOf(',');
        if (comma > 0)
            addrStr = addrStr.substring(0, comma);
        try {
            memsize = tokens.nextToken();
            if (memsize != null) {
                eq = memsize.indexOf('=');
                if (eq > 0)
                    memsize = memsize.substring(eq + 1);
            }
        } catch (ArrayIndexOutOfBoundsException e) {
        }
    }

    int month = dtime.monthOfYear().get();
    int day = dtime.monthOfYear().get();
    int hour = dtime.hourOfDay().get();
    int minute = dtime.minuteOfHour().get();
    int seconds = dtime.secondOfMinute().get();
    int millis = dtime.millisOfSecond().get();

    // construct a key
    String subject = "tstore:" + dtime.year().getAsText() + (month < 10 ? "0" + month : month)
            + (day < 10 ? "0" + day : day) + (hour < 10 ? "0" + hour : hour)
            + (minute < 10 ? "0" + minute : minute) + (seconds < 10 ? "0" + seconds : seconds) + "."
            + (millis < 100 ? "0" : "") + (millis < 10 ? "0" + millis : millis) + "_" + operation;

    ps.println(subject + "\t" + "tstore:timestamp" + "\t\"" + dtime.toLocalDateTime() + "\"^^xsd:dateTime .");
    ps.println(subject + "\t" + "tstore:type" + "\t\"" + operation + "\" .");
    ps.println(subject + "\t" + "tstore:address" + "\t\"" + addrStr + "\" .");
    if (memsize != null)
        ps.println(subject + "\t" + "tstore:memsize" + "\t\"" + memsize + "\"^^xsd:integer .");
    ps.println("");

}