Android Calendar Set modify(Calendar val, int field, boolean round)

Here you can find the source of modify(Calendar val, int field, boolean round)

Description

modify

Declaration

private static void modify(Calendar val, int field, boolean round) 

Method Source Code

import java.sql.Time;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.TimeZone;

public class Main{
    public final static int SEMI_MONTH = 1001;
    private static final int[][] FIELDS = { { Calendar.MILLISECOND },
            { Calendar.SECOND }, { Calendar.MINUTE },
            { Calendar.HOUR_OF_DAY, Calendar.HOUR },
            { Calendar.DATE, Calendar.DAY_OF_MONTH, Calendar.AM_PM },
            { Calendar.MONTH, DateTimeUtils.SEMI_MONTH },
            { Calendar.YEAR }, { Calendar.ERA } };
    private static void modify(Calendar val, int field, boolean round) {
        if (val.get(Calendar.YEAR) > 280000000) {
            throw new ArithmeticException(
                    "Calendar value too large for accurate calculations");
        }//w w w. jav  a2 s  .  c  o m

        if (field == Calendar.MILLISECOND) {
            return;
        }
        Date date = val.getTime();
        long time = date.getTime();
        boolean done = false;

        int millisecs = val.get(Calendar.MILLISECOND);
        if (!round || millisecs < 500) {
            time = time - millisecs;
            if (field == Calendar.SECOND) {
                done = true;
            }
        }

        int seconds = val.get(Calendar.SECOND);
        if (!done && (!round || seconds < 30)) {
            time = time - (seconds * 1000L);
            if (field == Calendar.MINUTE) {
                done = true;
            }
        }
        int minutes = val.get(Calendar.MINUTE);
        if (!done && (!round || minutes < 30)) {
            time = time - (minutes * 60000L);
        }

        if (date.getTime() != time) {
            date.setTime(time);
            val.setTime(date);
        }
        boolean roundUp = false;
        for (int i = 0; i < FIELDS.length; i++) {
            for (int j = 0; j < FIELDS[i].length; j++) {
                if (FIELDS[i][j] == field) {
                    if (round && roundUp) {
                        if (field == DateTimeUtils.SEMI_MONTH) {
                            if (val.get(Calendar.DATE) == 1) {
                                val.add(Calendar.DATE, 15);
                            } else {
                                val.add(Calendar.DATE, -15);
                                val.add(Calendar.MONTH, 1);
                            }
                        } else {
                            val.add(FIELDS[i][0], 1);
                        }
                    }
                    return;
                }
            }
            int offset = 0;
            boolean offsetSet = false;

            if (field == DateTimeUtils.SEMI_MONTH
                    && FIELDS[i][0] == Calendar.DATE) {
                offset = val.get(Calendar.DATE) - 1;
                if (offset >= 15) {
                    offset -= 15;
                }
                roundUp = offset > 7;
                offsetSet = true;
            } else if (field == Calendar.AM_PM
                    && FIELDS[i][0] == Calendar.HOUR_OF_DAY) {
                offset = val.get(Calendar.HOUR_OF_DAY);
                if (offset >= 12) {
                    offset -= 12;
                }
                roundUp = offset > 6;
                offsetSet = true;
            }

            if (!offsetSet) {
                int min = val.getActualMinimum(FIELDS[i][0]);
                int max = val.getActualMaximum(FIELDS[i][0]);
                offset = val.get(FIELDS[i][0]) - min;
                roundUp = offset > ((max - min) / 2);
            }
            if (offset != 0) {
                val.set(FIELDS[i][0], val.get(FIELDS[i][0]) - offset);
            }
        }
        throw new IllegalArgumentException("The field " + field
                + " is not supported");

    }
    public static Timestamp add(Date date, int calendarField, int amount) {
        if (date == null) {
            throw new IllegalArgumentException("The date must not be null");
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(calendarField, amount);
        return new Timestamp(c.getTimeInMillis());
    }
}

Related

  1. setTo2359(Calendar c)
  2. setToDec31(Calendar c)
  3. prepareCalendar(int hourOfDay, int minute)
  4. toDateTime(Calendar calendar)
  5. stripTime(Calendar cal)
  6. setDate(Calendar cal, long date)