⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stringutils.java

📁 java web模拟的简单的一个网络硬盘
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
		String strDate = formatter.format(myDate);
		return strDate;
	}

	public static String formatDate3(Date myDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("MM-dd HH:mm");
		String strDate = formatter.format(myDate);
		return strDate;
	}

	public static String formatDate4(Date myDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		String strDate = formatter.format(myDate);
		return strDate;
	}

	public static String formatDate5(Date myDate) {
		String strDate = getYear(myDate) + "-" + getMonth(myDate) + "-"
				+ getDay(myDate);
		return strDate;
	}

	public static String formatDate6(Date myDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String strDate = formatter.format(myDate);
		return strDate;
	}

	/**
	 * formatDateForRss(Date myDate) 格式化为RSS文件需要的rfc822规范
	 * 
	 * @param myDate
	 *            Date
	 * @return String
	 */
	public static String formatDateForRss(Date myDate) {
		SimpleDateFormat sdfTemp = new SimpleDateFormat(
				"EEE, d MMM yyyy HH:mm:ss z", Locale.US);
		SimpleTimeZone aZone = new SimpleTimeZone(8, "GMT");
		sdfTemp.setTimeZone(aZone);
		return sdfTemp.format(myDate);
	}

	public static long Date2Long(int year, int month, int date) {
		Calendar cld = Calendar.getInstance();
		month = month - 1;
		cld.set(year, month, date);
		return cld.getTime().getTime();
	}

	public static long Time2Long(int year, int month, int date, int hour,
			int minute, int second) {
		Calendar cld = Calendar.getInstance();
		month = month - 1;
		cld.set(year, month, date, hour, minute, second);
		return cld.getTime().getTime();
	}

	public static int getYear(long t) {
		Calendar cld = Calendar.getInstance();
		if (t > 0) {
			cld.setTime(new java.util.Date(t));
		}
		return cld.get(Calendar.YEAR);
	}

	public static int getMonth(long t) {
		Calendar cld = Calendar.getInstance();
		if (t > 0) {
			cld.setTime(new java.util.Date(t));
		}
		return cld.get(Calendar.MONTH) + 1;
	}

	public static int getDay(long t) {
		Calendar cld = Calendar.getInstance();
		if (t > 0) {
			cld.setTime(new java.util.Date(t));
		}
		return cld.get(Calendar.DAY_OF_MONTH);
	}

	public static int getHour(long t) {
		Calendar cld = Calendar.getInstance();
		if (t > 0) {
			cld.setTime(new java.util.Date(t));
		}
		return cld.get(Calendar.HOUR_OF_DAY);
	}

	public static int getMinute(long t) {
		Calendar cld = Calendar.getInstance();
		if (t > 0) {
			cld.setTime(new java.util.Date(t));
		}
		return cld.get(Calendar.MINUTE);
	}

	public static int getSecond(long t) {
		Calendar cld = Calendar.getInstance();
		if (t > 0) {
			cld.setTime(new java.util.Date(t));
		}
		return cld.get(Calendar.SECOND);
	}

	public static int getYear(Date date) {
		Calendar cld = Calendar.getInstance();
		cld.setTime(date);
		return cld.get(Calendar.YEAR);
	}

	public static int getMonth(Date date) {
		Calendar cld = Calendar.getInstance();
		cld.setTime(date);
		return cld.get(Calendar.MONTH) + 1;
	}

	public static int getDay(Date date) {
		Calendar cld = Calendar.getInstance();
		cld.setTime(date);
		return cld.get(Calendar.DAY_OF_MONTH);
	}

	public static int getHour(Date date) {
		Calendar cld = Calendar.getInstance();
		cld.setTime(date);
		return cld.get(Calendar.HOUR_OF_DAY);
	}

	public static int getMinute(Date date) {
		Calendar cld = Calendar.getInstance();
		cld.setTime(date);
		return cld.get(Calendar.MINUTE);
	}

	public static int getSecond(Date date) {
		Calendar cld = Calendar.getInstance();
		cld.setTime(date);
		return cld.get(Calendar.SECOND);
	}

	public static int getYear() {
		Calendar cld = Calendar.getInstance();
		cld.setTime(new java.util.Date());
		return cld.get(Calendar.YEAR);
	}

	public static int getMonth() {
		Calendar cld = Calendar.getInstance();
		cld.setTime(new java.util.Date());
		return cld.get(Calendar.MONTH) + 1;
	}

	public static int getDay() {
		Calendar cld = Calendar.getInstance();
		cld.setTime(new java.util.Date());
		return cld.get(Calendar.DAY_OF_MONTH);
	}

	public static String replaceComma(String text) {
		if (text != null) {
			text = text.replaceAll(",", ",");
		}
		return text;
	}

	public static String replaceBr(String text) {
		if (text != null) {
			text = text.replaceAll("\n", "<BR>");
			text = text.replaceAll("\n", "<br>");
		}
		return text;
	}

	public static long getLongTime() {
		return System.currentTimeMillis();
	}

	/**
	 * Check a string null or blank.
	 * 
	 * @param param
	 *            string to check
	 * @return boolean
	 */
	public static boolean nullOrBlank(String param) {
		return (param == null || param.length() == 0 || param.trim().equals("")) ? true
				: false;
	}

	public static String notNull(String param) {
		return param == null ? "" : param.trim();
	}

	/**
	 * Parse a string to boolean.
	 * 
	 * @param param
	 *            string to parse
	 * @return boolean value, if param begin with(1,y,Y,t,T) return true, on
	 *         exception return false.
	 */
	public static boolean parseBoolean(String param) {
		if (nullOrBlank(param)) {
			return false;
		}
		switch (param.charAt(0)) {
		case '1':
		case 'y':
		case 'Y':
		case 't':
		case 'T':
			return true;
		}
		return false;
	}

	/*
	 * public static String[] getPagesign(Locale locale) { String[] pagessign = {
	 * Messages.getMessage(locale, "pages.first"), Messages.getMessage(locale,
	 * "pages.previous"), Messages.getMessage(locale, "pages.next"),
	 * Messages.getMessage(locale, "pages.end")}; return pagessign; }
	 */

	/**
	 * dateToString(Date inDate) 把日期型转换成字符型"yyyy-MM-dd HH:mm:ss"
	 * 
	 * @param inDate
	 *            Date
	 * @return String
	 */
	public static String dateToString(Date inDate) {
		String outDateStr = "";
		if (inDate != null) {
			SimpleDateFormat formatter = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			outDateStr = formatter.format(inDate);
		}
		return outDateStr;
	}

	/***************************************************************************
	 * dateToSimpleStr 把日期型转换成字符型"yyyy-MM-dd"
	 * 
	 * @param inDate
	 *            Date 需要转换的日期时间
	 * @return outDateStr String
	 **************************************************************************/
	public static String dateToSimpleStr(Date inDate) {
		String outDateStr = "";
		if (inDate != null) {
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			outDateStr = formatter.format(inDate);
		}
		return outDateStr;
	}

	/***************************************************************************
	 * stringToDateWithTime 把字符型"yyyy-MM-dd HH:mm:ss"转换成日期型
	 * 
	 * @param s
	 *            String 需要转换的日期时间字符串
	 * @return theDate Date
	 **************************************************************************/
	public static Date stringToDateWithTime(String s) {
		Date theDate = new Date();
		try {
			if (s != null) {
				SimpleDateFormat dateFormatter = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");
				theDate = dateFormatter.parse(s);
			} else {
				theDate = null;
			}
		} catch (ParseException pe) {
			pe.printStackTrace();
		}
		return theDate;
	}

	/***************************************************************************
	 * stringToDate 把字符型"yyyy-MM-dd"转换成日期型
	 * 
	 * @param s
	 *            String 需要转换的日期时间字符串
	 * @return theDate Date
	 **************************************************************************/
	public static Date stringToDate(String s) {
		Date theDate = new Date();
		try {
			if (s != null) {
				SimpleDateFormat dateFormatter = new SimpleDateFormat(
						"yyyy-MM-dd");
				theDate = dateFormatter.parse(s);
			} else {
				theDate = null;
			}
		} catch (ParseException pe) {
			pe.printStackTrace();
		}
		return theDate;
	}

	/***************************************************************************
	 * dateAddInt Date +/- int = 新的Date
	 * 
	 * @param inDate
	 *            Date 原日期
	 * @param AddDateInt
	 *            int 要加减的天数
	 * @return ReturnDate Date 新的Date
	 **************************************************************************/
	@SuppressWarnings("static-access")
	public static Date dateAddInt(Date inDate, int AddDateInt) {
		Calendar currentC = Calendar.getInstance();
		currentC.setTime(inDate);
		currentC.add(currentC.DAY_OF_YEAR, AddDateInt);
		return currentC.getTime();
	}

	/***************************************************************************
	 * dayOfWeek 获得星期几
	 * 
	 * @param inDate
	 *            Date 原日期
	 * @param AddDateInt
	 *            int 要加减的天数
	 * @return dayOfWeek int 获得星期几
	 **************************************************************************/
	public static int dayOfWeek(Date inDate) {
		int dayOfWeek = 0;
		Calendar theCalendar = new GregorianCalendar();
		String DateStr = dateToString(inDate);
		theCalendar.set(Integer.parseInt(DateStr.substring(0, 4)), Integer
				.parseInt(DateStr.substring(5, 7)) - 1, Integer
				.parseInt(DateStr.substring(8, 10)));
		dayOfWeek = theCalendar.get(Calendar.DAY_OF_WEEK) - 1;
		if (dayOfWeek == 7) {
			dayOfWeek = 0;
		}
		return dayOfWeek;
	}

	/***************************************************************************
	 * minusDate 计算两个日期的相隔天数
	 * 
	 * @param beginDate
	 *            开始日期
	 * @param endDate
	 *            开始日期
	 * @return result long
	 **************************************************************************/
	public static long minusDate(Date beginDate, Date endDate) {
		long result = (beginDate.getTime() - endDate.getTime())
				/ (1000 * 60 * 60 * 24);
		return result;
	}

	/***************************************************************************
	 * getAge
	 * 
	 * @deprecated 根据生日计算年龄,周岁
	 * @param brithday
	 *            生日的字符串(yyyy-mm-dd)
	 * @return age int
	 **************************************************************************/
	public static int getAge(String brithday) {
		int age = 0;
		try {
			Calendar birth = Calendar.getInstance();
			int year = Integer.parseInt(brithday.substring(0, 4));
			int month = Integer.parseInt(brithday.substring(5, 7)) - 1;
			int day = Integer.parseInt(brithday.substring(8, 10));
			birth.set(year, month, day);
			Calendar today = Calendar.getInstance();
			if (today.get(Calendar.MONTH) > birth.get(Calendar.MONTH)
					|| (today.get(Calendar.MONTH) == birth.get(Calendar.MONTH))
					&& today.get(Calendar.DATE) >= birth.get(Calendar.DATE))
				age = today.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
			else
				age = today.get(Calendar.YEAR) - birth.get(Calendar.YEAR) - 1;
		} catch (Exception ex) {
			System.out.println("Error code:" + ex);
		}
		return age;
	}

	/***************************************************************************
	 * dateStrToLong
	 * 
	 * @see 2005-11-15
	 * @param dateStr
	 *            日期字符串yyyy-mm-dd
	 * @return long theDay
	 **************************************************************************/
	public static long dateStrToLong(String dateStr) {
		long theDate = getLongTime();
		Date thisDate = stringToDate(dateStr);
		if (thisDate != null) {
			theDate = thisDate.getTime();
		}
		return theDate;
	}

	/***************************************************************************
	 * dateStrWithTimeToLong
	 * 
	 * @param dateStr
	 *            日期字符串yyyy-MM-dd HH:mm:ss
	 * @return long theDay
	 **************************************************************************/
	public static long dateStrWithTimeToLong(String dateStr) {
		long theDate = getLongTime();
		Date thisDate = stringToDateWithTime(dateStr);
		if (thisDate != null) {
			theDate = thisDate.getTime();
		}
		return theDate;
	}

	/***************************************************************************
	 * longToDateStr
	 * 
	 * @param long
	 *            theDateLong
	 * @return String DateStr yyyy-MM-dd
	 **************************************************************************/
	public static String longToDateStr(long theDateLong) {
		String dateStr = "1970-01-01";
		try {
			dateStr = Integer.toString(getYear(theDateLong)) + "-"
					+ getMonth(theDateLong) + "-" + getDay(theDateLong);
		} catch (Exception ex) {
			System.out.println("error code:" + ex);
		}
		return dateStr;
	}

	/***************************************************************************
	 * longToDateStr
	 * 
	 * @param long
	 *            theDateLong
	 * @return String DateStr yyyy-MM-dd
	 **************************************************************************/
	public static String longToDateWithTimeStr(long theDateLong) {
		String dateStr = "1970-01-01 00:00:00";
		try {
			dateStr = Integer.toString(getYear(theDateLong)) + "-"
					+ getMonth(theDateLong) + "-" + getDay(theDateLong) + " "
					+ getHour(theDateLong) + ":" + getMinute(theDateLong) + ":"
					+ getSecond(theDateLong);
		} catch (Exception ex) {
			System.out.println("error code:" + ex);
		}
		return dateStr;
	}

	/**
	 * 反转数组
	 * 
	 * @param src
	 *            源数组
	 * @return 转换后的数组
	 */
	public static int[] ArrayConvert(int[] src) {
		if (src == null) {
			return null;
		}
		int length = src.length;
		int temp = 0;
		// 反转
		for (int i = 0; i < length / 2; i++) {
			temp = src[i];
			src[i] = src[length - i - 1];
			src[length - i - 1] = temp;
		}
		return src;
	}

	public static void main(String[] args) {
		/*
		 * String t = "abcdfg\"sfdfaeeed<a href=>ghjfghj"; t =
		 * escapeForSpecial(t); System.out.println(t);
		 * System.out.println(unescapeFromXML(t));
		 */
		Date theDate = new Date();
		System.out.println(theDate.getTime());
		String temp = dateToSimpleStr(theDate);
		System.out.println(temp);
		long ldate = dateStrToLong(temp);
		System.out.println(ldate);
		System.out.println(getYear(ldate));
		System.out.println(getMonth(ldate));
		System.out.println(getDay(ldate));
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -