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:org.apache.roller.weblogger.ui.core.tags.calendar.WeblogCalendarModel.java

/**
 * Parse data as either 6-char or 8-char format.
 *///from  ww w .j  a va2  s  .  com
public static Date parseWeblogURLDateString(String dateString, TimeZone tz, Locale locale) {

    Date ret = new Date();
    Calendar cal = Calendar.getInstance(tz, locale);

    if (dateString != null && dateString.length() == 8 && StringUtils.isNumeric(dateString)) {
        SimpleDateFormat char8DateFormat = DateUtil.get8charDateFormat();
        char8DateFormat.setCalendar(cal);
        ParsePosition pos = new ParsePosition(0);
        ret = char8DateFormat.parse(dateString, pos);

        // make sure the requested date is not in the future
        //            Date today = null;
        //            Calendar todayCal = Calendar.getInstance();
        //            todayCal = Calendar.getInstance(tz, locale);
        //            todayCal.setTime(new Date());
        //            today = todayCal.getTime();
        // Date is always ms offset from epoch in UTC, by no means of timezone.
        Date today = new Date();
        if (ret.after(today)) {
            ret = today;
        }

    } else if (dateString != null && dateString.length() == 6 && StringUtils.isNumeric(dateString)) {
        SimpleDateFormat char6DateFormat = DateUtil.get6charDateFormat();
        char6DateFormat.setCalendar(cal);
        ParsePosition pos = new ParsePosition(0);
        ret = char6DateFormat.parse(dateString, pos);

        // make sure the requested date is not in the future
        //            Calendar todayCal = Calendar.getInstance();
        //            todayCal = Calendar.getInstance(tz, locale);
        //            todayCal.setTime(new Date());
        //            Date today = todayCal.getTime();
        Date today = new Date();
        if (ret.after(today)) {
            ret = today;
        }
    }

    return ret;
}

From source file:jp.classmethod.aws.brian.job.BrianQuartzJobBean.java

static SimpleDateFormat createFormat() {
    SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
    Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Japan")); // TODO localize
    calendar.setMinimalDaysInFirstWeek(4);
    calendar.setFirstDayOfWeek(Calendar.MONDAY);
    calendar.setTimeInMillis(0);/*from   ww w  .ja  va 2  s. co m*/
    format.setCalendar(calendar);
    return format;
}

From source file:com.icesoft.tutorial.TimeZoneBean.java

public static DateFormat buildDateFormatForTimeZone(TimeZone timeZone) {
    SimpleDateFormat currentFormat = new SimpleDateFormat("EEE, HH:mm:ss");
    Calendar currentZoneCal = Calendar.getInstance(timeZone);
    currentFormat.setCalendar(currentZoneCal);
    currentFormat.setTimeZone(timeZone);
    return currentFormat;
}

From source file:com.krawler.common.timezone.Timezone.java

public static String[] getGmtDate(Connection conn, String date1, String userid)
        throws ServiceException, ParseException {

    String tzUser = getTimeZone(conn, userid);

    Calendar calInstance = Calendar.getInstance();
    calInstance.setTimeZone(java.util.TimeZone.getTimeZone("GMT" + tzUser));
    TimeZone tz = calInstance.getTimeZone();
    int temp = tz.getRawOffset();
    java.text.SimpleDateFormat format0 = new SimpleDateFormat("yyyy-MM-d HH:mm:ss");
    java.text.SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-d HH:mm:ss");
    java.util.Calendar cal0 = Calendar.getInstance(new SimpleTimeZone(0, "GMT"));
    java.util.Calendar cal1 = Calendar.getInstance(new SimpleTimeZone(temp, tzUser));
    format0.setCalendar(cal0);
    format1.setCalendar(cal1);//from  w w w.j  a v a  2 s .  co  m
    String dateArr[] = date1.split(" ");
    if (dateArr.length == 1 || dateArr[1].equals("00:00:00")) {
        date1 = dateArr[0] + " " + "00:00:01";
    }
    java.util.Date date = format1.parse(date1);
    String result = format0.format(date);

    String[] results = result.split(" ");
    return results;
}

From source file:com.krawler.common.timezone.Timezone.java

public static String[] getTzonetoGmt(String date1, String tzUser) throws ServiceException, ParseException {

    Calendar calInstance = Calendar.getInstance();
    calInstance.setTimeZone(java.util.TimeZone.getTimeZone("GMT" + tzUser));
    TimeZone tz = calInstance.getTimeZone();
    int temp = tz.getRawOffset();
    java.text.SimpleDateFormat format0 = new SimpleDateFormat("yyyy-MM-d HH:mm:ss");
    java.text.SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-d HH:mm:ss");
    java.util.Calendar cal0 = Calendar.getInstance(new SimpleTimeZone(0, "GMT"));
    java.util.Calendar cal1 = Calendar.getInstance(new SimpleTimeZone(temp, tzUser));
    format0.setCalendar(cal0);
    format1.setCalendar(cal1);//  w w w . j a  v  a2 s  . c o  m
    String dateArr[] = date1.split(" ");
    if (dateArr[1].equals("00:00:00")) {
        date1 = dateArr[0] + " " + "00:00:01";
    }
    java.util.Date date = format1.parse(date1);
    String result = format0.format(date);

    String[] results = result.split(" ");
    if (results[1].equals("00:00:00")) {
        results[1] = "00:00:01";
    }
    return results;
}

From source file:org.openmrs.module.atomfeed.AtomFeedUtil.java

/**
 * Format dates as specified in rfc3339 (required for Atom dates)
 * /*from   w  ww .  j a v a 2 s .  com*/
 * @param d the Date to be formatted
 * @return the formatted date
 * @should not fail given a null date
 * @should convert date to rfc
 */
public static String dateToRFC3339(Date d) {
    if (d == null)
        return null;
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(RFC_3339_DATE_FORMAT);
    Calendar cal = new GregorianCalendar();
    cal.setTime(d);
    cal.setTimeZone(TimeZone.getDefault());
    simpleDateFormat.setCalendar(cal);
    StringBuilder result = new StringBuilder(simpleDateFormat.format(d));
    int offset_millis = cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET);
    int offset_hours = Math.abs(offset_millis / (1000 * 60 * 60));
    int offset_minutes = Math.abs((offset_millis / (1000 * 60)) % 60);

    if (offset_millis == 0) {
        result.append("Z");
    } else {
        result.append((offset_millis > 0) ? "+" : "-").append(doubleDigit.format(offset_hours)).append(":")
                .append(doubleDigit.format(offset_minutes));
    }

    return result.toString();
}

From source file:org.nuxeo.drive.operations.NuxeoDriveGenerateConflictedItemName.java

@OperationMethod
public Blob run() throws Exception {

    String extension = "";
    if (name.contains(".")) {
        // Split on the last occurrence of . using a negative lookahead
        // regexp.
        String[] parts = name.split("\\.(?=[^\\.]+$)");
        name = parts[0];/*  w ww .  j av a  2  s  .  c om*/
        extension = "." + parts[1];
    }
    NuxeoPrincipal principal = (NuxeoPrincipal) ctx.getPrincipal();
    String userName = principal.getName(); // fallback
    if (!StringUtils.isBlank(principal.getLastName()) && !StringUtils.isBlank(principal.getFirstName())) {
        // build more user friendly name from user info
        userName = principal.getFirstName() + " " + principal.getLastName();
    }
    Calendar userDate;
    if (timezone != null) {
        userDate = Calendar.getInstance(TimeZone.getTimeZone(timezone));
    } else {
        userDate = Calendar.getInstance();
    }
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh-mm");
    dateFormat.setCalendar(userDate);
    String formatedDate = dateFormat.format(userDate.getTime());
    String contextSection = String.format(" (%s - %s)", userName, formatedDate);
    String conflictedName = name + contextSection + extension;
    return NuxeoDriveOperationHelper.asJSONBlob(conflictedName);
}

From source file:org.dasein.cloud.aws.storage.Glacier.java

private static long parseTimestamp(String timestamp) {
    if (timestamp == null) {
        return -1;
    }//from  ww  w  .j a  v  a 2  s.  c om
    long creationTs;
    SimpleDateFormat fmt;

    // some response dates have MS component, some do not.
    if (timestamp.contains(".")) {
        fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    } else {
        fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    }
    Calendar cal = Calendar.getInstance(new SimpleTimeZone(0, "GMT"));
    fmt.setCalendar(cal);
    try {
        creationTs = fmt.parse(timestamp).getTime();
    } catch (ParseException e) {
        creationTs = System.currentTimeMillis();
    }
    return creationTs;
}

From source file:net.sourceforge.jcctray.model.CCNet.java

private SimpleDateFormat getDateFormatter(TimeZone timeZone) {
    SimpleDateFormat dateFormatter = new SimpleDateFormat("h:mm:ss a, dd MMM", LOCALE_US);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeZone(timeZone);/*ww w  .ja  v  a 2 s  .c  o  m*/
    dateFormatter.setCalendar(calendar);
    return dateFormatter;
}

From source file:org.sakaiproject.tool.assessment.ui.listener.util.TimeUtil.java

/**
* Convert a Date representation of date and time in the server TimeZone 
* to String representation of date and time  in client TimeZone
* used for display. //from  w  w w .j  a v  a  2 s.  c  o m
* tz1 is the client timezone,  tz2 is the server timezone
*/

public String convertFromServerDateToTimeZone2String(SimpleDateFormat ndf, Date tz2Date, TimeZone tz1) {
    // for display
    Calendar cal1 = new GregorianCalendar(tz1);
    ndf.setCalendar(cal1);
    String clientStr = ndf.format(tz2Date);

    return clientStr;
}