Example usage for java.util Calendar get

List of usage examples for java.util Calendar get

Introduction

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

Prototype

public int get(int field) 

Source Link

Document

Returns the value of the given calendar field.

Usage

From source file:Main.java

public static long getTimeMillisAsHourAndMinute(long millis) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(millis);/*from w  ww. j  a  v  a 2  s .  co m*/
    int hour = calendar.get(Calendar.HOUR_OF_DAY);
    int minute = calendar.get(Calendar.MINUTE);
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, minute);
    return calendar.getTimeInMillis();
}

From source file:Main.java

public static int getYearFromDate(String origDate) {
    DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
    Date date;//  w ww  .  j  av  a2  s  . c o  m
    int year = 1;
    try {
        date = formatter.parse(origDate);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        year = cal.get(Calendar.YEAR);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return year;
}

From source file:Main.java

/**
 * Returns a date formatted in ANSI SQL.
 *
 * @param date the Date.//from   ww w . j  a  v a 2  s.c o  m
 * @return a date String.
 */
public static String getSqlDateString(Date date) {
    Calendar cal = Calendar.getInstance();

    cal.setTime(date);

    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH) + 1;
    int day = cal.get(Calendar.DAY_OF_MONTH);

    String yearString = String.valueOf(year);
    String monthString = month < 10 ? "0" + month : String.valueOf(month);
    String dayString = day < 10 ? "0" + day : String.valueOf(day);

    return yearString + "-" + monthString + "-" + dayString;
}

From source file:Main.java

public static String getDateString(long millitm) {
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(millitm);/*w  w  w  .ja  v a 2s.c  om*/
    return (setZeroPad(cal.get(Calendar.YEAR)) + "/" + setZeroPad(cal.get(Calendar.MONTH)) + "/"
            + setZeroPad(cal.get(Calendar.DATE)) + " " + setZeroPad(cal.get(Calendar.HOUR_OF_DAY)) + ":"
            + setZeroPad(cal.get(Calendar.MINUTE)) + ":" + setZeroPad(cal.get(Calendar.SECOND)));
}

From source file:Main.java

public static java.util.Date getDateStart(java.util.Date d) {

    Calendar c = new GregorianCalendar();
    c.clear();/*from w ww . j  av a2 s  .  c  o m*/
    Calendar co = new GregorianCalendar();
    co.setTime(d);
    c.set(Calendar.DAY_OF_MONTH, co.get(Calendar.DAY_OF_MONTH));
    c.set(Calendar.MONTH, co.get(Calendar.MONTH));
    c.set(Calendar.YEAR, co.get(Calendar.YEAR));
    // c.add(Calendar.DAY_OF_MONTH,1);
    // c.add(Calendar.MILLISECOND,-1);
    return c.getTime();
}

From source file:Main.java

public static int getMonthFromDate(String origDate) {
    DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
    Date date;/*w  w  w .  ja v  a 2s  .  c  o m*/
    int month = 1;
    try {
        date = formatter.parse(origDate);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        month = cal.get(Calendar.MONTH);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return month;
}

From source file:Main.java

public static boolean isAfterADay(Calendar newCalendar, Calendar oldCalendar) {
    Log.d("TEST", "run isAferDay");
    if (newCalendar.get(Calendar.YEAR) > oldCalendar.get(Calendar.YEAR))
        return true;
    if (newCalendar.get(Calendar.YEAR) == oldCalendar.get(Calendar.YEAR)
            && newCalendar.get(Calendar.MONTH) > oldCalendar.get(Calendar.MONTH))
        return true;
    if (newCalendar.get(Calendar.YEAR) == oldCalendar.get(Calendar.YEAR)
            && newCalendar.get(Calendar.MONTH) == oldCalendar.get(Calendar.MONTH)
            && newCalendar.get(Calendar.DAY_OF_MONTH) > oldCalendar.get(Calendar.DAY_OF_MONTH))
        return true;
    return false;
}

From source file:Main.java

public static int getDatePart(Date date, int part) {
    if (date == null)
        return 0;
    Calendar cal = Calendar.getInstance(Locale.getDefault());
    cal.setTime(date);// w w  w . j  a  v a2s  .c  o m
    return cal.get(part);
}

From source file:Main.java

/**
 * yyyyMMddHHmmss//from  w  ww.j ava2  s  .  co  m
 */
public static String getFormatTime3(Calendar c) {
    DecimalFormat df = new DecimalFormat("00");
    String strFileName = c.get(Calendar.YEAR) + df.format((c.get(Calendar.MONTH) + 1))
            + df.format(c.get(Calendar.DAY_OF_MONTH)) + df.format(c.get(Calendar.HOUR_OF_DAY))
            + df.format(c.get(Calendar.MINUTE)) + df.format(c.get(Calendar.SECOND));

    return strFileName;
}

From source file:Main.java

public static String date2String(Date date, boolean printTime) {
    Calendar cal = new GregorianCalendar();
    cal.setTime(date);//from w w  w .  j  ava 2 s  .com
    if (cal.get(Calendar.YEAR) > 10000) {
        return "---";
    }
    return makeTimeString(cal.get(Calendar.DAY_OF_MONTH), 2) + "."
            + makeTimeString(cal.get(Calendar.MONTH) + 1, 2) + "." + makeTimeString(cal.get(Calendar.YEAR), 4)
            + (printTime
                    ? " " + makeTimeString(cal.get(Calendar.HOUR_OF_DAY), 2) + ":"
                            + makeTimeString(cal.get(Calendar.MINUTE), 2) + ":"
                            + makeTimeString(cal.get(Calendar.SECOND), 2)
                    : "");
}