Example usage for java.text SimpleDateFormat setCalendar

List of usage examples for java.text SimpleDateFormat setCalendar

Introduction

In this page you can find the example usage for java.text SimpleDateFormat setCalendar.

Prototype

public void setCalendar(Calendar newCalendar) 

Source Link

Document

Set the calendar to be used by this date format.

Usage

From source file:Main.java

public static String shortFormatTime(Calendar time) {
    SimpleDateFormat f = new SimpleDateFormat("EEE, h:mm a");
    f.setCalendar(time);

    return f.format(time.getTime());
}

From source file:Main.java

public static String longFormatTime(Calendar time) {
    SimpleDateFormat f = new SimpleDateFormat("EEE, MMM d, h:mm a");
    f.setCalendar(time);

    return f.format(time.getTime());
}

From source file:Main.java

/**
 * Convert given calendar to formated string 
 * @param calendar to convert/*from w ww.  j a va  2  s. com*/
 * @return formated date in string
 */
@SuppressLint("SimpleDateFormat")
public static String calendarToString(Calendar calendar) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    dateFormat.setCalendar(calendar);
    return dateFormat.format(calendar.getTime());
}

From source file:Main.java

public static String formatTime(String formatString, long msSinceEpoch) {
    SimpleDateFormat dateFormat = new SimpleDateFormat(formatString);
    Calendar cal = Calendar.getInstance();
    dateFormat.setCalendar(cal);
    return dateFormat.format(new Date(msSinceEpoch));
}

From source file:Main.java

/**
 * Formats a birthday date to a String    
 * @param bDay date in <i>"yyyy-mm-dd"</i> format
 * @return formated date string </br><b>Format : </b> "yyyy-MM-dd HH:mm:ss" 
 *///from   www.  j  av  a2 s.co  m
public static String formatContactDate(String bDay) {
    Calendar calDob = Calendar.getInstance();
    String formated = "";
    if (bDay != null) {
        String[] mmddyyyy = bDay.split("-");
        if (mmddyyyy.length == 3) {
            calDob.set(calDob.get(Calendar.YEAR), Integer.parseInt(mmddyyyy[1]) - 1,
                    Integer.parseInt(mmddyyyy[2]), 23, 59, 59);
        } else if (mmddyyyy.length == 2) {
            calDob.set(calDob.get(Calendar.YEAR), Integer.parseInt(mmddyyyy[1]) - 1,
                    Integer.parseInt(mmddyyyy[2]), 23, 59, 59);
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
        dateFormat.setCalendar(calDob);
        formated = dateFormat.format(calDob.getTime());
    }
    return formated;
}

From source file:Main.java

/**
 * Formats a birthday date to a String    
 * @param bDay date in <i>"mm/dd/yyyy"</i> format
 * @return formated date string </br><b>Format : </b> "yyyy-MM-dd HH:mm:ss" 
 *///w w  w .j a  va 2 s  .  com
public static String formatDate(String bDay) {
    Calendar calDob = Calendar.getInstance();
    String formated = "";
    if (bDay != null) {
        String[] mmddyyyy = bDay.split("/");
        if (mmddyyyy.length == 3) {
            calDob.set(calDob.get(Calendar.YEAR), Integer.parseInt(mmddyyyy[0]) - 1,
                    Integer.parseInt(mmddyyyy[1]), 23, 59, 59);
        } else if (mmddyyyy.length == 2) {
            calDob.set(calDob.get(Calendar.YEAR), Integer.parseInt(mmddyyyy[0]) - 1,
                    Integer.parseInt(mmddyyyy[1]), 23, 59, 59);
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
        dateFormat.setCalendar(calDob);
        formated = dateFormat.format(calDob.getTime());
    }
    return formated;
}

From source file:org.vpac.ndg.Utils.java

/**
 * Tries a list of allowable date formats when parsing the specified date.
 * /*from   w  w  w . java 2  s. c  o m*/
 * @param str
 *            A string representation of the date.
 * @return The parsed date.
 * @throws IllegalArgumentException
 *             if the date can't be parsed.
 */
public static Date parseDate(String str) throws IllegalArgumentException {
    Date result = null;

    SimpleDateFormat formatter = new SimpleDateFormat();
    formatter.setCalendar(Calendar.getInstance(TimeZone.getTimeZone("UTC")));
    for (String pattern : StringUtils.ALLOWABLE_DATETIME_PATTERNS) {
        try {
            formatter.applyPattern(pattern);
            result = formatter.parse(str);
            if (result != null) {
                break;
            }
        } catch (ParseException e) {
        } catch (IllegalArgumentException e) {
        }
    }

    if (result == null) {
        throw new IllegalArgumentException(String.format("Could not parse date \"%s\".", str));
    }

    return result;
}

From source file:io.horizondb.model.core.util.TimeUtils.java

/**
 * Parses the specified <code>String</code> representing a date/time.
 * <p>The supported patterns are: 'yyyy-MM-dd', 'yyyy-MM-dd HH:mm:ss' and 
 * 'yyyy-MM-dd HH:mm:ss.SSS'.</p>//  ww  w .ja va  2 s. c  o  m
 * 
 * @param timeZone the date TimeZone
 * @param dateTime the <code>String</code> representing a date/time.
 * @return the time in millisecond since epoch
 */
public static long parseDateTime(TimeZone timeZone, String dateTime) {

    String pattern = getPattern(dateTime);
    SimpleDateFormat format = new SimpleDateFormat(pattern);

    Calendar calendar = Calendar.getInstance(timeZone);
    calendar.clear();
    calendar.setLenient(false);

    format.setCalendar(calendar);

    Date date = format.parse(dateTime, new ParsePosition(0));

    if (date == null) {
        throw new IllegalArgumentException(format("The value %s cannot be parsed into a valid date", dateTime));
    }
    return date.getTime();
}

From source file:org.apache.roller.weblogger.ui.core.tags.calendar.WeblogCalendarModel.java

public static String format8chars(Date date, Calendar cal) {
    SimpleDateFormat format = DateUtil.get8charDateFormat();
    format.setCalendar(cal);
    return DateUtil.format(date, format);
}

From source file:org.apache.roller.weblogger.ui.core.tags.calendar.WeblogCalendarModel.java

public static String format6chars(Date date, Calendar cal) {
    SimpleDateFormat format = DateUtil.get6charDateFormat();
    format.setCalendar(cal);
    return DateUtil.format(date, format);
}