Example usage for java.text DateFormat format

List of usage examples for java.text DateFormat format

Introduction

In this page you can find the example usage for java.text DateFormat format.

Prototype

public final String format(Date date) 

Source Link

Document

Formats a Date into a date-time string.

Usage

From source file:br.msf.commons.util.AbstractDateUtils.java

public static String formatDate(final Object date, final Locale locale) {
    final DateFormat df = SimpleDateFormat.getDateInstance(DateFormat.MEDIUM,
            LocaleUtils.getNullSafeLocale(locale));
    return df.format(castToDate(date));
}

From source file:br.msf.commons.util.AbstractDateUtils.java

public static String formatTime(final Object date, final Locale locale) {
    final DateFormat df = SimpleDateFormat.getTimeInstance(DateFormat.MEDIUM,
            LocaleUtils.getNullSafeLocale(locale));
    return df.format(castToDate(date));
}

From source file:com.eurotong.orderhelperandroid.Common.java

public static String downloadContentFromInternet(String urlString) throws IOException {
    /*/*from  w  w w.  j a va  2 s.co m*/
    URLConnection feedUrl=null;
    try
    {
       feedUrl = new URL(url).openConnection();
    }
    catch (MalformedURLException e)
    {
       Log.v("ERROR","MALFORMED URL EXCEPTION");
    }
    catch (IOException e) {
       e.printStackTrace();
    }
    try
    {
       InputStream in = feedUrl.getInputStream();
       String json = convertStreamToString(in);
       return  json;
    }
    catch(Exception e){}
    return null;
    */
    //http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

    StrictMode.setThreadPolicy(policy);

    DateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
    Date date = new Date();
    String datetime = dateFormat.format(date);
    // Create a URL for the desired page
    URL url = new URL(urlString + "?d=" + datetime);

    // Read all the text returned by the server

    InputStream in = url.openStream();
    String content = convertStreamToString(in);
    return content;
}

From source file:MimeUtil.java

/**
 * Formats the specified date into a RFC 822 date-time string.
 * /*from ww w  . ja  v  a 2 s . c  om*/
 * @param date
 *            date to be formatted into a string.
 * @param zone
 *            the time zone to use or <code>null</code> to use the default
 *            time zone.
 * @return the formatted time string.
 */
public static String formatDate(Date date, TimeZone zone) {
    DateFormat df = RFC822_DATE_FORMAT.get();

    if (zone == null) {
        df.setTimeZone(TimeZone.getDefault());
    } else {
        df.setTimeZone(zone);
    }

    return df.format(date);
}

From source file:at.bitfire.ical4android.DateUtils.java

/**
 * Concatenates, if necessary, multiple RDATE/EXDATE lists and converts them to
 * a formatted string which Android calendar provider can process.
 * Android expects this format: "[TZID;]date1,date2,date3" where date is "yyyymmddThhmmss" (when
 * TZID is given) or "yyyymmddThhmmssZ". We don't use the TZID format here because then we're limited
 * to one time-zone, while an iCalendar may contain multiple EXDATE/RDATE lines with different time zones.
 * @param dates      one more more lists of RDATE or EXDATE
 * @param allDay    indicates whether the event is an all-day event or not
 * @return         formatted string for Android calendar provider:
 *                  - in case of all-day events, all dates/times are returned as yyyymmddT000000Z
 *                  - in case of timed events, all dates/times are returned as UTC time: yyyymmddThhmmssZ
 *///from   w  w  w .java  2  s . c  o m
public static String recurrenceSetsToAndroidString(List<? extends DateListProperty> dates, boolean allDay)
        throws ParseException {
    List<String> strDates = new LinkedList<>();

    /*        rdate/exdate: DATE                                DATE_TIME
        all-day             store as ...T000000Z                cut off time and store as ...T000000Z
        event with time     (ignored)                           store as ...ThhmmssZ
    */
    final DateFormat dateFormatUtcMidnight = new SimpleDateFormat("yyyyMMdd'T'000000'Z'", Locale.US);

    for (DateListProperty dateListProp : dates) {
        final Value type = dateListProp.getDates().getType();

        if (Value.DATE_TIME.equals(type)) { // DATE-TIME values will be stored in UTC format for Android
            if (allDay) {
                DateList dateList = dateListProp.getDates();
                for (Date date : (Iterable<Date>) dateList)
                    strDates.add(dateFormatUtcMidnight.format(date));
            } else {
                dateListProp.setUtc(true);
                strDates.add(dateListProp.getValue());
            }

        } else if (Value.DATE.equals(type)) // DATE values have to be converted to DATE-TIME <date>T000000Z for Android
            for (Date date : (Iterable<Date>) dateListProp.getDates())
                strDates.add(dateFormatUtcMidnight.format(date));
    }
    return StringUtils.join(strDates, ",");
}

From source file:br.msf.commons.util.AbstractDateUtils.java

public static String formatDateTime(final Object date, final Locale locale) {
    final DateFormat df = SimpleDateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM,
            LocaleUtils.getNullSafeLocale(locale));
    return df.format(castToDate(date));
}

From source file:com.fluidops.iwb.api.ValueResolver.java

/**
 * Converts a system date like '2011-03-31T19:54:33' to user-readable date.
 * If the input is not a valid system date, the value is returned as is.
 * /*from   ww w .j  a  v  a  2 s .c  o  m*/
 * @param sysdate
 * @return
 */
public static String resolveSysDate(String sysdate) {
    Date d = ReadDataManagerImpl.ISOliteralToDate(sysdate);
    if (d == null)
        return StringEscapeUtils.escapeHtml(sysdate);

    DateFormat df = null;
    if (d.getHours() == 0 && d.getMinutes() == 0 && d.getSeconds() == 0)
        df = new SimpleDateFormat("MMMMM dd, yyyy");
    else
        df = new SimpleDateFormat("MMMMM dd, yyyy, HH:mm:ss");
    return df.format(d);
}

From source file:com.amalto.core.storage.StorageMetadataUtils.java

public static String toString(Object o, FieldMetadata field) {
    if (o == null) {
        return StringUtils.EMPTY;
    }/* w w  w  . java  2  s. c o  m*/
    if (field instanceof ReferenceFieldMetadata) {
        return toString(o);
    }
    TypeMetadata type = MetadataUtils.getSuperConcreteType(field.getType());
    String typeName = type.getName();
    if (Types.DATE.equals(typeName)) {
        synchronized (DateConstant.DATE_FORMAT) {
            try {
                DateFormat dateFormat = DateConstant.DATE_FORMAT;
                return dateFormat.format(o);
            } catch (Exception e) {
                throw new RuntimeException("Could not parse date '" + o + "'.", e);
            }
        }
    } else if (Types.DATETIME.equals(typeName)) {
        synchronized (DateTimeConstant.DATE_FORMAT) {
            try {
                DateFormat dateFormat = DateTimeConstant.DATE_FORMAT;
                return dateFormat.format(o);
            } catch (Exception e) {
                throw new RuntimeException("Could not parse date time '" + o + "'.", e);
            }
        }
    } else if (Types.TIME.equals(typeName)) {
        synchronized (TimeConstant.TIME_FORMAT) {
            try {
                DateFormat dateFormat = TimeConstant.TIME_FORMAT;
                return dateFormat.format(o);
            } catch (Exception e) {
                throw new RuntimeException("Could not parse time '" + o + "'.", e);
            }
        }
    } else {
        return String.valueOf(o);
    }
}

From source file:com.krawler.common.util.SchedulingUtilities.java

public static String getFormattedDateString(String date, String format) {
    Date d = null;//from   w  w w  .  j  a  va 2s .c  o m
    DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    if (!StringUtil.isNullOrEmpty(format))
        sdf = new SimpleDateFormat(format);
    try {
        d = DateUtils.parseDate(date, new String[] { "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd" });
    } catch (ParseException e) {
        throw ServiceException.FAILURE("Date Parse Exception :: " + e.getMessage(), e);
    } finally {
        if (d != null)
            return sdf.format(d);
        else
            return date;
    }
}

From source file:com.radvision.icm.service.vcm.ICMService.java

private static long getCurrentDate(long time) throws Exception {
    Date dt = new Date(time);
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    String s = df.format(dt);
    Date dtNew = df.parse(s);/* ww  w . ja  v a 2 s  .  c  o  m*/
    return dtNew.getTime();
}