Java Code Examples for java.text.SimpleDateFormat#getDateInstance()

The following examples show how to use java.text.SimpleDateFormat#getDateInstance() . 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: BeanPropertyComparatorTest.java    From rice with Educational Community License v2.0 6 votes vote down vote up
@Test
/**
 * test the comparison of null objects
 */
public void testCompare_firstNullDates() throws ParseException {
	List propertiesD = Arrays.asList(new String[] { "d" });
	
	DateFormat dateFormat = SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT);
	
	BeanPropertyComparator comparator = new BeanPropertyComparator(propertiesD);
	
	D lesser = new D(null);
	D greater = new D(dateFormat.parse("01/02/1990"));
	
	int result = comparator.compare(greater, lesser);
	Assert.assertEquals(1, result);
	
	result = comparator.compare(lesser, greater);
	Assert.assertEquals(-1, result);
	
	result = comparator.compare(lesser, lesser);
	Assert.assertEquals(0, result);
	
	result = comparator.compare(greater, greater);
	Assert.assertEquals(0, result);
}
 
Example 2
Source File: DateTimeService.java    From apiman with Apache License 2.0 6 votes vote down vote up
/**
 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 */
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
        IOException {
    resp.setContentType("application/json"); //$NON-NLS-1$
    ServletOutputStream outputStream = resp.getOutputStream();
    PrintWriter writer = new PrintWriter(outputStream);
    Date date = new Date();
    DateFormat dateFormat = SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT);
    
    writer.println("{"); //$NON-NLS-1$
    writer.println("  \"date\" : \"" + dateFormat.format(date) + "\""); //$NON-NLS-1$ //$NON-NLS-2$
    writer.println("}"); //$NON-NLS-1$
    writer.flush();
    writer.close();
}
 
Example 3
Source File: BeanPropertyComparatorTest.java    From rice with Educational Community License v2.0 6 votes vote down vote up
@Test
/**
 * test the comparison of null objects
 */
public void testCompare_secondNullDates() throws ParseException {
	List propertiesD = Arrays.asList(new String[] { "d" });
	
	DateFormat dateFormat = SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT);
	
	BeanPropertyComparator comparator = new BeanPropertyComparator(propertiesD);
	
	D lesser = new D(dateFormat.parse("01/02/1990"));
	D greater = new D(null);
	
	int result = comparator.compare(greater, lesser);
	Assert.assertEquals(-1, result);
	
	result = comparator.compare(lesser, greater);
	Assert.assertEquals(1, result);
	
	result = comparator.compare(lesser, lesser);
	Assert.assertEquals(0, result);
	
	result = comparator.compare(greater, greater);
	Assert.assertEquals(0, result);
}
 
Example 4
Source File: GenericExportWorker.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
public void setDateAndDecimalLocale(Locale dateAndDecimalLocale) {
	dateFormat = SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT, dateAndDecimalLocale);
	dateTimeFormat = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.SHORT, SimpleDateFormat.MEDIUM, dateAndDecimalLocale);
	((SimpleDateFormat) dateTimeFormat).applyPattern(((SimpleDateFormat) dateTimeFormat).toPattern().replaceFirst("y+", "yyyy").replaceFirst(", ", " "));
	dateFormatter = null;
	dateTimeFormatter = null;
	decimalFormat = DecimalFormat.getNumberInstance(dateAndDecimalLocale);
	decimalFormat.setGroupingUsed(false);
}
 
Example 5
Source File: SimpleConvertersProvider.java    From spearal-java with Apache License 2.0 5 votes vote down vote up
private static Date parseDate(String s) {
	// TODO: support ISO-8601 format.
	DateFormat format = SimpleDateFormat.getDateInstance(DateFormat.SHORT, Locale.US);
	try {
		return format.parse(s);
	}
	catch (Exception e) {
		return null;
	}
}
 
Example 6
Source File: DateUtil.java    From Klyph with MIT License 5 votes vote down vote up
private static SimpleDateFormat getDateFormat()
{
	SimpleDateFormat dateFormat = (SimpleDateFormat) SimpleDateFormat.getDateInstance(SimpleDateFormat.FULL);
	String pattern = dateFormat.toLocalizedPattern();

	pattern = pattern.replace("y", "");
	pattern = pattern.replace("E", "");
	pattern = pattern.replace(",", "");
	pattern = pattern.replace("  ", " ");
	pattern = pattern.trim();

	dateFormat.applyLocalizedPattern(pattern);

	return dateFormat;
}
 
Example 7
Source File: TransactionViewActivityTest.java    From budget-watch with GNU General Public License v3.0 5 votes vote down vote up
@Before
public void setUp() throws ParseException
{
    // Output logs emitted during tests so they may be accessed
    ShadowLog.stream = System.out;

    final DateFormat dateFormatter = SimpleDateFormat.getDateInstance();
    nowString = dateFormatter.format(System.currentTimeMillis());
    nowMs = dateFormatter.parse(nowString).getTime();
}
 
Example 8
Source File: DateStep.java    From material-singleinputform with MIT License 5 votes vote down vote up
private void updateText(){
	String output = "";
	if(year >= 0 && month >= 0 && day >= 0){
		DateFormat format = SimpleDateFormat.getDateInstance();
		output = format.format(new GregorianCalendar(year, month, day).getTime());
	}
	setText(output);
}
 
Example 9
Source File: StringUtil.java    From LittleFreshWeather with Apache License 2.0 5 votes vote down vote up
public static Date stringToDate(String pattern, String in) {
    SimpleDateFormat simpleDateFormat = (SimpleDateFormat)SimpleDateFormat.getDateInstance();
    simpleDateFormat.applyPattern(pattern);

    Date retDate = null;
    try {
        retDate = simpleDateFormat.parse(in);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return retDate;
}
 
Example 10
Source File: ComAdminImpl.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public SimpleDateFormat getDateFormat() {
	SimpleDateFormat dateFormat = (SimpleDateFormat) SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT, getLocale());
	dateFormat.applyPattern(dateFormat.toPattern().replaceFirst("y+", "yyyy"));
	dateFormat.setTimeZone(TimeZone.getTimeZone(getAdminTimezone()));
	dateFormat.setLenient(false);
	return dateFormat;
}
 
Example 11
Source File: GenericExportWorker.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
public GenericExportWorker() {
   	Locale dateAndDecimalLocale = Locale.getDefault();
   	
	// Create the default number format
	decimalFormat = DecimalFormat.getNumberInstance(dateAndDecimalLocale);
	decimalFormat.setGroupingUsed(false);

	// Create the default date format
	dateFormat = SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT, dateAndDecimalLocale);
	((SimpleDateFormat) dateFormat).applyPattern(((SimpleDateFormat) dateFormat).toPattern().replaceFirst("y+", "yyyy"));

	// Create the default date and time format
	dateTimeFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, dateAndDecimalLocale);
	((SimpleDateFormat) dateTimeFormat).applyPattern(((SimpleDateFormat) dateTimeFormat).toPattern().replaceFirst("y+", "yyyy").replaceFirst(", ", " "));
}
 
Example 12
Source File: StringUtil.java    From LittleFreshWeather with Apache License 2.0 4 votes vote down vote up
public static String getFriendlyDateString(Date date, boolean showDayOfWeek) {
    if (date == null) {
        if (showDayOfWeek)
            return "--.--. 周--";
        else
            return "--.--.";
    }

    GregorianCalendar nowCalender = new GregorianCalendar();
    GregorianCalendar dstCalender = new GregorianCalendar();
    dstCalender.setTime(date);

    int now = nowCalender.get(GregorianCalendar.DAY_OF_YEAR);
    int dst = dstCalender.get(GregorianCalendar.DAY_OF_YEAR);
    int dstDayOfWeek = dstCalender.get(GregorianCalendar.DAY_OF_WEEK);
    String dstDayOfWeekString = "周--";
    if (showDayOfWeek) {
        switch (dstDayOfWeek) {
            case 1: {
                dstDayOfWeekString = "周日";
            } break;
            case 2: {
                dstDayOfWeekString = "周一";
            } break;
            case 3: {
                dstDayOfWeekString = "周二";
            } break;
            case 4: {
                dstDayOfWeekString = "周三";
            } break;
            case 5: {
                dstDayOfWeekString = "周四";
            } break;
            case 6: {
                dstDayOfWeekString = "周五";
            } break;
            case 7: {
                dstDayOfWeekString = "周六";
            } break;
        }
    }

    if (dst - now == 0) {
        if (showDayOfWeek)
            return "今天" + " " + dstDayOfWeekString;
        else
            return "今天";
    } else if (dst - now == 1) {
        if (showDayOfWeek)
            return "明天" + " " + dstDayOfWeekString;
        else
            return "明天";
    } else if (dst - now == 2) {
        if (showDayOfWeek)
            return "后天" + " " + dstDayOfWeekString;
        else
            return "后天";
    } else if (dst - now == -1) {
        if (showDayOfWeek)
            return "昨天" + " " + dstDayOfWeekString;
        else
            return "昨天";
    } else if (dst - now == -2) {
        if (showDayOfWeek)
            return "前天" + " " + dstDayOfWeekString;
        else
            return "前天";
    } else {
        SimpleDateFormat simpleDateFormat = (SimpleDateFormat)SimpleDateFormat.getDateInstance();
        if (showDayOfWeek)
            simpleDateFormat.applyPattern("M.d EE");
        else
            simpleDateFormat.applyPattern("M.d");
        return simpleDateFormat.format(date);
    }
}
 
Example 13
Source File: DateTimeConverter.java    From octoandroid with GNU General Public License v3.0 4 votes vote down vote up
public static String msTimeToShortDateString(long msTime) {
    Date date = new Date(msTime);
    DateFormat df = SimpleDateFormat.getDateInstance(DateFormat.SHORT);
    return df.format(date);
}
 
Example 14
Source File: AgeValidator.java    From RxValidator with Apache License 2.0 4 votes vote down vote up
public AgeValidator() {
  message = DEFAULT_MESSAGE;
  age = DEFAULT_AGE;
  sdf = SimpleDateFormat.getDateInstance();
}
 
Example 15
Source File: Helper.java    From FairEmail with GNU General Public License v3.0 4 votes vote down vote up
static DateFormat getDateInstance(Context context) {
    return SimpleDateFormat.getDateInstance(SimpleDateFormat.MEDIUM);
}
 
Example 16
Source File: NewEventActivity.java    From RoomBookerMVP with MIT License 4 votes vote down vote up
private void initDateTimeFormat() {
    simpleTimeFormat = SimpleDateFormat.getTimeInstance(SimpleDateFormat.SHORT);
    simpleDateFormat = SimpleDateFormat.getDateInstance();
}
 
Example 17
Source File: TimeUtils.java    From actor-platform with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
protected DateFormat initialValue() {
    return SimpleDateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
}
 
Example 18
Source File: MainActivity.java    From FlyRefresh with MIT License 4 votes vote down vote up
public ItemAdapter(Context context) {
    mInflater = LayoutInflater.from(context);
    dateFormat = SimpleDateFormat.getDateInstance(DateFormat.DEFAULT, Locale.ENGLISH);
}
 
Example 19
Source File: DatePickerDialog.java    From material with Apache License 2.0 4 votes vote down vote up
private boolean isMonthFirst(){
    SimpleDateFormat format = (SimpleDateFormat)SimpleDateFormat.getDateInstance(SimpleDateFormat.FULL);
    String pattern = format.toLocalizedPattern();

    return pattern.indexOf("M") < pattern.indexOf("d");
}
 
Example 20
Source File: EasyConfigMod.java    From easydeviceinfo with Apache License 2.0 2 votes vote down vote up
/**
 * Gets formatted date.
 *
 * @return the formatted date
 */
public final String getFormattedDate() {
  DateFormat dateInstance = SimpleDateFormat.getDateInstance();
  return dateInstance.format(Calendar.getInstance().getTime());
}