Java Code Examples for org.apache.commons.lang3.time.FastDateFormat#format()

The following examples show how to use org.apache.commons.lang3.time.FastDateFormat#format() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseDateTimeType.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
/**
 * This method implements a datetime equality check using the rules as defined by FHIRPath (R2)
 *
 * Caveat: this implementation assumes local timezone for unspecified timezones 
 */
public Boolean equalsUsingFhirPathRules(BaseDateTimeType theOther) {
  TemporalPrecisionEnum mp = this.myPrecision == TemporalPrecisionEnum.MILLI ? TemporalPrecisionEnum.SECOND : this.myPrecision;
  TemporalPrecisionEnum op = theOther.myPrecision == TemporalPrecisionEnum.MILLI ? TemporalPrecisionEnum.SECOND : theOther.myPrecision;
  TemporalPrecisionEnum cp = (mp.compareTo(op) < 0) ? mp : op;
  FastDateFormat df = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSS");
  String ms = df.format(this.getValue());
  String os = df.format(theOther.getValue());
  if (!sub(ms, cp.stringLength()).equals(sub(os, cp.stringLength()))) {
    return false;
  }
  if (mp != op) {
    return null;
  }
  if (this.myPrecision == TemporalPrecisionEnum.MILLI || theOther.myPrecision == TemporalPrecisionEnum.MILLI) {
    float mf = Float.parseFloat(ms.substring(17)); 
    float of = Float.parseFloat(os.substring(17));
    if (mf != of) {
      return false;
    }
  }
  return true;
}
 
Example 2
Source File: DateCustom.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Converts a date to a string, returning the "date" portion using the operating system's locale's conventions.
 * @param context the JavaScript context
 * @param thisObj the scriptable
 * @param args the arguments passed into the method
 * @param function the function
 * @return converted string
 */
public static String toLocaleDateString(
        final Context context, final Scriptable thisObj, final Object[] args, final Function function) {
    final String formatString;
    final BrowserVersion browserVersion = ((Window) thisObj.getParentScope()).getBrowserVersion();

    if (browserVersion.hasFeature(JS_DATE_WITH_LEFT_TO_RIGHT_MARK)) {
        // [U+200E] -> Unicode Character 'LEFT-TO-RIGHT MARK'
        formatString = "\u200EM\u200E/\u200Ed\u200E/\u200Eyyyy";
    }
    else if (browserVersion.hasFeature(JS_DATE_LOCALE_DATE_SHORT)) {
        formatString = "M/d/yyyy";
    }
    else {
        formatString = "EEEE, MMMM dd, yyyy";
    }
    final FastDateFormat format =  FastDateFormat.getInstance(formatString, getLocale(browserVersion));
    return format.format(getDateValue(thisObj));
}
 
Example 3
Source File: DateCustom.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Converts a date to a string, returning the "time" portion using the current locale's conventions.
 * @param context the JavaScript context
 * @param thisObj the scriptable
 * @param args the arguments passed into the method
 * @param function the function
 * @return converted string
 */
public static String toLocaleTimeString(
        final Context context, final Scriptable thisObj, final Object[] args, final Function function) {
    final String formatString;
    final BrowserVersion browserVersion = ((Window) thisObj.getParentScope()).getBrowserVersion();

    if (browserVersion.hasFeature(JS_DATE_WITH_LEFT_TO_RIGHT_MARK)) {
        // [U+200E] -> Unicode Character 'LEFT-TO-RIGHT MARK'
        formatString = "\u200Eh\u200E:\u200Emm\u200E:\u200Ess\u200E \u200Ea";
    }
    else {
        formatString = "h:mm:ss a";
    }
    final FastDateFormat format =  FastDateFormat.getInstance(formatString, getLocale(browserVersion));
    return format.format(getDateValue(thisObj));
}
 
Example 4
Source File: DateCustom.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Converts a date to a string, returning the "date" portion using the operating system's locale's conventions.
 * @param context the JavaScript context
 * @param thisObj the scriptable
 * @param args the arguments passed into the method
 * @param function the function
 * @return converted string
 */
public static String toLocaleDateString(
        final Context context, final Scriptable thisObj, final Object[] args, final Function function) {
    final String formatString;
    final BrowserVersion browserVersion = ((Window) thisObj.getParentScope()).getBrowserVersion();

    if (browserVersion.hasFeature(JS_DATE_WITH_LEFT_TO_RIGHT_MARK)) {
        // [U+200E] -> Unicode Character 'LEFT-TO-RIGHT MARK'
        formatString = "\u200EM\u200E/\u200Ed\u200E/\u200Eyyyy";
    }
    else if (browserVersion.hasFeature(JS_DATE_LOCALE_DATE_SHORT)) {
        formatString = "M/d/yyyy";
    }
    else {
        formatString = "EEEE, MMMM dd, yyyy";
    }
    final FastDateFormat format =  FastDateFormat.getInstance(formatString, getLocale(browserVersion));
    return format.format(getDateValue(thisObj));
}
 
Example 5
Source File: DateCustom.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Converts a date to a string, returning the "time" portion using the current locale's conventions.
 * @param context the JavaScript context
 * @param thisObj the scriptable
 * @param args the arguments passed into the method
 * @param function the function
 * @return converted string
 */
public static String toLocaleTimeString(
        final Context context, final Scriptable thisObj, final Object[] args, final Function function) {
    final String formatString;
    final BrowserVersion browserVersion = ((Window) thisObj.getParentScope()).getBrowserVersion();

    if (browserVersion.hasFeature(JS_DATE_WITH_LEFT_TO_RIGHT_MARK)) {
        // [U+200E] -> Unicode Character 'LEFT-TO-RIGHT MARK'
        formatString = "\u200Eh\u200E:\u200Emm\u200E:\u200Ess\u200E \u200Ea";
    }
    else {
        formatString = "h:mm:ss a";
    }
    final FastDateFormat format =  FastDateFormat.getInstance(formatString, getLocale(browserVersion));
    return format.format(getDateValue(thisObj));
}
 
Example 6
Source File: DateUtil.java    From KlyphMessenger with MIT License 5 votes vote down vote up
public static String getShortDateTime(String unixDate)
	{
		Date date = new Date(Long.parseLong(unixDate)*1000);
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		c2.setTime(date);
		
		
		FastDateFormat dateFormat = FastDateFormat.getDateTimeInstance(FastDateFormat.MEDIUM, FastDateFormat.SHORT);
		String pattern = dateFormat.getPattern();
		
		// If not same year
		if (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR))
		{
			pattern = pattern.replace("y", "");
			
			if (pattern.indexOf("/") == 0 || pattern.indexOf("-") == 0 || pattern.indexOf(".") == 0  || pattern.indexOf("年") == 0)
			{
				pattern = pattern.substring(1);
			}
			
/*			pattern = pattern.replace("EEEE", "EEE");
			pattern = pattern.replace("MMMM", "");
			pattern = pattern.replace("d", "");
		}
		else
		{
			pattern = pattern.replace("MMMM", "MMM");
			pattern = pattern.replace("EEEE", "");*/
		}
		
		pattern = pattern.replace("  ", " ");
		pattern = pattern.trim();
		
		dateFormat = FastDateFormat.getInstance(pattern);
		
		return dateFormat.format(date);
	}
 
Example 7
Source File: GetChangelistsDateRangeTest.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
private String oneWeekBefore() {
    FastDateFormat dateFormat = FastDateFormat.getInstance("@yyyy/MM/dd");

    Calendar now = Calendar.getInstance();
    now.add(Calendar.WEEK_OF_YEAR, -1);

    return dateFormat.format(now);
}
 
Example 8
Source File: CachingDateFormatter.java    From vjtools with Apache License 2.0 5 votes vote down vote up
public CachingDateFormatter(FastDateFormat fastDateFormat) {
	this.fastDateFormat = fastDateFormat;
	onSecond = fastDateFormat.getPattern().indexOf("SSS") == -1;

	long current = System.currentTimeMillis();
	this.cachedTime = new AtomicReference<CachedTime>(new CachedTime(current, fastDateFormat.format(current)));
}
 
Example 9
Source File: CachingDateFormatter.java    From vjtools with Apache License 2.0 5 votes vote down vote up
public CachingDateFormatter(FastDateFormat fastDateFormat) {
	this.fastDateFormat = fastDateFormat;
	onSecond = fastDateFormat.getPattern().indexOf("SSS") == -1;

	long current = System.currentTimeMillis();
	this.cachedTime = new AtomicReference<CachedTime>(new CachedTime(current, fastDateFormat.format(current)));
}
 
Example 10
Source File: GetChangelistsDateRangeTest.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
private String oneWeekBefore() {
    FastDateFormat dateFormat = FastDateFormat.getInstance("@yyyy/MM/dd");

    Calendar now = Calendar.getInstance();
    now.add(Calendar.WEEK_OF_YEAR, -1);

    return dateFormat.format(now);
}
 
Example 11
Source File: GetChangelistsDateRangeTest.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
private String oneWeekBefore() {
    FastDateFormat dateFormat = FastDateFormat.getInstance("@yyyy/MM/dd");

    Calendar now = Calendar.getInstance();
    now.add(Calendar.WEEK_OF_YEAR, -1);

    return dateFormat.format(now);
}
 
Example 12
Source File: GetChangelistsDateRangeTest.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
private String oneWeekBefore() {
    FastDateFormat dateFormat = FastDateFormat.getInstance("@yyyy/MM/dd");

    Calendar now = Calendar.getInstance();
    now.add(Calendar.WEEK_OF_YEAR, -1);

    return dateFormat.format(now);
}
 
Example 13
Source File: CachingDateFormatter.java    From j360-dubbo-app-all with Apache License 2.0 5 votes vote down vote up
public CachingDateFormatter(FastDateFormat fastDateFormat) {
	this.fastDateFormat = fastDateFormat;
	onSecond = fastDateFormat.getPattern().indexOf("SSS") == -1;

	long current = System.currentTimeMillis();
	this.cachedTime = new AtomicReference<CachedTime>(new CachedTime(current, fastDateFormat.format(current)));
}
 
Example 14
Source File: DateUtil.java    From alibaba-flink-connectors with Apache License 2.0 4 votes vote down vote up
public static String date2String(Date value, String timeZone, @Nonnull String format) {
	FastDateFormat sdf = getDateFormat(timeZone, format);
	return sdf.format(value);
}
 
Example 15
Source File: DateUtils.java    From RxJava2RetrofitDemo with Apache License 2.0 4 votes vote down vote up
public static String convert2yyyy_MM_dd_china(long dateMillionSeconds) {
	Date dateTime = new Date(dateMillionSeconds);
	FastDateFormat formatter = yyyy年MM月dd日;
	return formatter.format(dateTime);
}
 
Example 16
Source File: DateUtils.java    From RxJava2RetrofitDemo with Apache License 2.0 4 votes vote down vote up
public static String convert2yyyy_MM_dd(long dateMillionSeconds) {
	Date dateTime = new Date(dateMillionSeconds);
	FastDateFormat formatter = YYYY_MM_DD;
	return formatter.format(dateTime);
}
 
Example 17
Source File: DateUtils.java    From RxJava2RetrofitDemo with Apache License 2.0 4 votes vote down vote up
public static String convert2yyyy_MM_dd_HH_mm(long dateMillionSeconds) {
	Date dateTime = new Date(dateMillionSeconds);
	FastDateFormat formatter = YYYY_MM_DD_HH_MM;
	return formatter.format(dateTime);
}
 
Example 18
Source File: DateUtils.java    From RxJava2RetrofitDemo with Apache License 2.0 4 votes vote down vote up
public static String convert2yyyy_MM_dd_HH_mm_ss(long dateMillionSeconds) {
	Date dateTime = new Date(dateMillionSeconds);
	FastDateFormat formatter = YYYY_MM_DD_HH_MM_SS;
	return formatter.format(dateTime);
}
 
Example 19
Source File: DateFormat.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
private static String formatToStrWithTimeZone(TimeZone timeZone, long mills, String pattern){
    FastDateFormat dateFormat =  FastDateFormat.getInstance(pattern, timeZone);
    return dateFormat.format(new Date(mills));
}
 
Example 20
Source File: EncoderUtils.java    From james-project with Apache License 2.0 2 votes vote down vote up
/**
 * Encodes a date in IMAP <code>date-time</code> format.
 * 
 * @param date
 *            <code>Date</code>, not null
 * @return encoded IMAP <code>date-time</code>, not null
 */
public static String encodeDateTime(Date date) {
    final FastDateFormat format = FastDateFormat.getInstance("dd-MMM-yyyy HH:mm:ss Z", TimeZone.getTimeZone("GMT"), Locale.US);
    return format.format(date);
}