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

/**
 * Get First Date of month/*from w  w w. ja  v  a  2  s  .com*/
 */
public static int getDayFromDate(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar.get(Calendar.DAY_OF_WEEK);
}

From source file:Main.java

public static String getCurrentDateString(Calendar calendar) {
    if (calendar == null)
        return "";
    return calendar.get(Calendar.YEAR) + "-" + String.format("%02d", (calendar.get(Calendar.MONTH) + 1)) + "-"
            + String.format("%02d", calendar.get(Calendar.DATE));
}

From source file:Main.java

public static int getWeekOfYear(long timeInMillis) {
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(timeInMillis);/* www .  j  av  a  2  s .c om*/
    return cal.get(Calendar.WEEK_OF_YEAR);
}

From source file:Main.java

public static final int getMonthByMill(long mill) {
    Date curDate = new Date(mill);
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(curDate);/*from   w  ww  . ja  va2 s . c o  m*/
    return calendar.get(java.util.Calendar.MONTH) + 1;
}

From source file:Main.java

public static final int getYearByMill(long mill) {
    Date curDate = new Date(mill);
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(curDate);/*from   w  w  w  .  ja  va 2  s  .co  m*/
    return calendar.get(java.util.Calendar.YEAR);
}

From source file:Main.java

public static int[] formatTimeToArray(long time) {
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(time);/*from w w w. j av a 2  s .  co m*/
    int hour = cal.get(Calendar.HOUR_OF_DAY);
    int minute = cal.get(Calendar.MINUTE);
    return new int[] { hour, minute };
}

From source file:Main.java

public static Date getDateBefore(Date d, int day) {
    Calendar now = Calendar.getInstance();
    now.setTime(d);/*  w ww  . j  a v a  2s .  com*/
    now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
    return now.getTime();
}

From source file:Main.java

public static final int getDayByMill(long mill) {
    Date curDate = new Date(mill);
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(curDate);/*from  w w  w  .ja v a 2  s  .  c  om*/
    return calendar.get(java.util.Calendar.DAY_OF_MONTH);
}

From source file:Main.java

public static String getSeqWeek() {
    Calendar c = Calendar.getInstance(Locale.CHINA);
    String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
    if (week.length() == 1)
        week = "0" + week;
    String year = Integer.toString(c.get(Calendar.YEAR));
    return year + week;
}

From source file:Main.java

public static String getCurrentTimeString(int s) {
    //s=0 for star time s=1 for end time
    Calendar currentCalendar = Calendar.getInstance();
    int hours = currentCalendar.get(Calendar.HOUR_OF_DAY);
    int minutes = currentCalendar.get(Calendar.MINUTE);

    if (minutes < 30) {
        currentCalendar.setTimeInMillis(currentCalendar.getTimeInMillis() + (30 - minutes) * 60 * 1000);
    }/*from   www  .ja v a 2  s  .c om*/
    if (minutes > 30) {
        currentCalendar.setTimeInMillis(currentCalendar.getTimeInMillis() + (60 - minutes) * 60 * 1000);
    }
    if (s == 1)
        currentCalendar.setTimeInMillis(currentCalendar.getTimeInMillis() + 30 * 60 * 1000);

    return timeFormat.format(currentCalendar.getTimeInMillis());
}