Example usage for java.util Calendar clone

List of usage examples for java.util Calendar clone

Introduction

In this page you can find the example usage for java.util Calendar clone.

Prototype

@Override
public Object clone() 

Source Link

Document

Creates and returns a copy of this object.

Usage

From source file:TripServlet.java

public Date getLastWeek() {
    Calendar c = Calendar.getInstance();
    c = (Calendar) c.clone();
    c.add(Calendar.WEEK_OF_YEAR, -1);// last week
    c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
    Date date = c.getTime();/*from   ww w . ja  va  2  s  .c  o m*/
    return date;
}

From source file:TripServlet.java

public Date getLastMonth() {
    Calendar c = Calendar.getInstance();
    c = (Calendar) c.clone();
    c.add(Calendar.MONTH, -1);// last month
    c.set(Calendar.DAY_OF_MONTH, c.getFirstDayOfWeek());
    Date date = c.getTime();//  ww w.java 2s . c om
    return date;
}

From source file:TripServlet.java

public Date getLastYear() {
    Calendar c = Calendar.getInstance();
    c = (Calendar) c.clone();
    c.add(Calendar.YEAR, -1);// last month
    c.set(Calendar.DAY_OF_YEAR, c.getFirstDayOfWeek());
    Date date = c.getTime();//from   w  w w  .ja  v  a2  s .com
    return date;
}

From source file:nl.strohalm.cyclos.utils.TimePeriod.java

/**
 * Returns a period ending at the given date (not including it), with this size
 *//*from w w  w .ja va 2s .c om*/
public Period periodEndingAt(Calendar endDate) {
    endDate = (Calendar) endDate.clone();
    final Calendar beginDate = remove(endDate);
    endDate.add(Calendar.SECOND, -1);
    return Period.between(beginDate, endDate);
}

From source file:com.cognizant.trumobi.em.Email.java

public static String getWeekDays() {
    Calendar cal = Calendar.getInstance();
    String string = Email.getPresentDay();
    // Log.e("getPresentDay", ""+string);
    Date date = null;//from  w  w w. jav  a 2s  . c om
    try {
        date = new SimpleDateFormat("dd MMM yyyy", Locale.ENGLISH).parse(string);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    cal.setTime(date);
    SimpleDateFormat format = new SimpleDateFormat("MMM dd", Locale.US);
    Calendar firstCal = (Calendar) cal.clone();

    firstCal.add(Calendar.DAY_OF_WEEK, firstCal.getFirstDayOfWeek() - firstCal.get(Calendar.DAY_OF_WEEK));
    // SharedPreferences prefs = PreferenceManager
    // .getDefaultSharedPreferences(appContext);
    SharedPreferences prefs = new SharedPreferences(appContext);
    String WeekStartsOn = prefs.getString(appContext.getString(R.string.key_list_preference), "");
    if (WeekStartsOn.equalsIgnoreCase("2")) {
        firstCal.setFirstDayOfWeek(Calendar.SATURDAY);
    } else if (WeekStartsOn.equalsIgnoreCase("3")) {
        firstCal.setFirstDayOfWeek(Calendar.SUNDAY);
    } else if (WeekStartsOn.equalsIgnoreCase("4")) {
        firstCal.setFirstDayOfWeek(Calendar.MONDAY);
    }
    int firstDayOfWeek = firstCal.getFirstDayOfWeek();
    int dayOfWeek = firstCal.get(Calendar.DAY_OF_WEEK);
    if (firstDayOfWeek == 7) {
        firstCal.add(Calendar.DAY_OF_WEEK, -dayOfWeek);
    } else
        firstCal.add(Calendar.DAY_OF_WEEK, firstCal.getFirstDayOfWeek() - firstCal.get(Calendar.DAY_OF_WEEK));
    String[] days = new String[2];
    days[0] = format.format(firstCal.getTime());
    Calendar lastCal = (Calendar) firstCal.clone();
    lastCal.add(Calendar.DAY_OF_YEAR, 6);
    days[1] = format.format(lastCal.getTime());
    String[] weekDays = Arrays.toString(days).split(",");
    // String weekVal2=weekDays[1].substring(weekDays[1].length()-1);
    String weekVal2 = weekDays[1].replace("]", "");
    String weekVal = weekDays[0].substring(1) + " - " + weekVal2;
    return weekVal;
}

From source file:com.github.xlstosql.SqlGenerator.java

private void readTables() {
    result = new StringBuilder();
    try {/*w w w .  j a v  a 2  s  . c  o  m*/
        for (String fileName : files) {
            Workbook workbook = Workbook.getWorkbook(new File(fileName));
            List<String> sheetNames = new ArrayList<String>();
            for (Sheet sheet : workbook.getSheets()) {
                sheetNames.add(sheet.getName());
                headers = sheet.getRow(0);
                for (int indexRow = 1; indexRow < sheet.getRows(); indexRow += 1) {
                    sheetName = sheet.getName();
                    appendHeaders();
                    Cell[] datas = sheet.getRow(indexRow);
                    if (datas != null && datas.length > 0) {
                        result.append(translateValue(0, cellToString(datas[0])));
                    }
                    for (int indexCell = 1; indexCell < headers.length; indexCell += 1) {
                        if (isValidDictHeader(headers[indexCell].getContents())) {
                            result.append(", " + translateValue(indexCell, cellToString(datas[indexCell])));
                        }
                    }
                    if (isDict()) {
                        Calendar today = Calendar.getInstance();
                        Calendar from = (Calendar) today.clone();
                        Calendar to = (Calendar) today.clone();
                        from.add(Calendar.DATE, -1000);
                        to.add(Calendar.DATE, 1000);
                        result.append(", '" + sheetName.substring(5) + "', 1, " + toSqlFormat(from) + ", "
                                + toSqlFormat(to) + ", " + toSqlFormat(today));
                    }
                    result.append(")\n");
                }
            }
            workbook.close();
            IOUtils.write(result.toString(), new FileOutputStream(outSql), "cp1251");
            getLog().log(Level.INFO, "read " + fileName.replaceFirst(".*/(.*)", "$1") + " - " + sheetNames);
        }
    } catch (Exception ex) {
        getLog().log(Level.WARNING, ex.getMessage(), ex);
    }
}

From source file:com.bdb.weather.display.stripchart.StripChart.java

/**
 * Adjust the range of the domain axis based on the current time and the span selected by the user.
 * /*from  w  w w .  j  av  a2 s. co  m*/
 * @param t0 The current time
 */
private void adjustDateAxis(Calendar t0) {
    Calendar now = t0;
    Calendar intercept = (Calendar) now.clone();
    intercept.add(Calendar.HOUR, -span);
    now.add(Calendar.MINUTE, 5);
    dateAxis.setRange(intercept.getTime(), now.getTime());
}

From source file:MonthPanel.java

protected JPanel createDaysGUI() {
    JPanel dayPanel = new JPanel(true);
    dayPanel.setLayout(new GridLayout(0, dayNames.length));

    Calendar today = Calendar.getInstance();
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.MONTH, month);
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.DAY_OF_MONTH, 1);

    Calendar iterator = (Calendar) calendar.clone();
    iterator.add(Calendar.DAY_OF_MONTH, -(iterator.get(Calendar.DAY_OF_WEEK) - 1));

    Calendar maximum = (Calendar) calendar.clone();
    maximum.add(Calendar.MONTH, +1);

    for (int i = 0; i < dayNames.length; i++) {
        JPanel dPanel = new JPanel(true);
        dPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        JLabel dLabel = new JLabel(dayNames[i]);
        dPanel.add(dLabel);/*from w  ww  . ja  va 2s  . c  om*/
        dayPanel.add(dPanel);
    }

    int count = 0;
    int limit = dayNames.length * 6;

    while (iterator.getTimeInMillis() < maximum.getTimeInMillis()) {
        int lMonth = iterator.get(Calendar.MONTH);
        int lYear = iterator.get(Calendar.YEAR);

        JPanel dPanel = new JPanel(true);
        dPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        JLabel dayLabel = new JLabel();

        if ((lMonth == month) && (lYear == year)) {
            int lDay = iterator.get(Calendar.DAY_OF_MONTH);
            dayLabel.setText(Integer.toString(lDay));
        }
        dPanel.add(dayLabel);
        dayPanel.add(dPanel);
        iterator.add(Calendar.DAY_OF_YEAR, +1);
        count++;
    }

    for (int i = count; i < limit; i++) {
        JPanel dPanel = new JPanel(true);
        dPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));

        dPanel.add(new JLabel());
        dayPanel.add(dPanel);
    }
    return dayPanel;
}

From source file:nl.strohalm.cyclos.dao.access.LoginHistoryDAOImpl.java

public List<Pair<User, Number>> list(final StatisticalDTO dto) throws DaoException, EntityNotFoundException {
    final Map<String, Object> namedParameters = new HashMap<String, Object>();
    final StringBuilder hql = new StringBuilder();

    hql.append(" select new " + Pair.class.getName() + "(mu, count(lhl.id)) ");
    hql.append(" from MemberUser mu ");
    hql.append(" join mu.element m ");
    hql.append(" left join mu.loginHistory lhl ");
    hql.append(" where 1=1 ");

    // Period// w  w  w.j a  v a  2 s.co m
    Calendar begin = dto.getPeriod().getBegin();
    Calendar end = dto.getPeriod().getEnd();
    if (dto.getPeriod().isUseTime()) {
        if (end != null) {
            // Add a second to end
            end = (Calendar) end.clone();
            end.add(Calendar.SECOND, 1);
        }
    } else {
        // Truncate both dates
        begin = DateHelper.truncate(begin);
        end = DateHelper.truncateNextDay(end);
    }
    namedParameters.put("begin", begin);
    namedParameters.put("end", end);
    hql.append("and (lhl.date is null or (lhl.date >= :begin and lhl.date < :end))");

    // Member groups
    if (!CollectionUtils.isEmpty(dto.getGroups())) {
        hql.append(" and exists ");
        hql.append(" ( ");
        hql.append("    select ghl.id from GroupHistoryLog ghl ");
        hql.append("    where ghl.element.id = m.id ");
        hql.append("    and ghl.group in (:groups) ");
        hql.append("    and ghl.period.begin < :end ");
        hql.append("    and (ghl.period.end is null or ghl.period.end >= :begin) ");
        hql.append(
                "    and (lhl.date is null or lhl.date between ghl.period.begin and ifnull(ghl.period.end, lhl.date)) ");
        hql.append(" ) ");
        namedParameters.put("groups", dto.getGroups());
    }

    // hql.append(" group by m.user ");
    hql.append(" group by mu ");
    hql.append(" order by count(lhl.id) desc ");
    final List<Pair<User, Number>> logs = list(hql.toString(), namedParameters);
    return logs;
}

From source file:com.samknows.measurement.net.Connection.java

private String dates(int difference) {
    Calendar end = Calendar.getInstance();
    Calendar start = (Calendar) end.clone();
    start.add(Calendar.DATE, -difference);
    return datesToString(start, end);
}