StringUtils.java 17 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
package com.ectrip.cyt.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Currency;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.Random;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;

public class StringUtils {
	public final static String[] weeks = { "周日", "周一", "周二", "周三", "周四", "周五",
			"周六" };
	public final static String[] weeks2 = {  "周一", "周二", "周三", "周四", "周五",
			"周六" ,"周日" };
	public static final String TAG = "StringUtils";

	public static String getRandonNumber() {
		Long h = System.currentTimeMillis();// 获得当前时间的毫秒数
		String str = h.toString();// 转化为字符串
		int i = str.length();
		int j = i - 7;// 用来取此字符串的末尾7位数,因为前面的数是年份�?么的基本不变,我们只用后面的7�?
		String body = str.substring(j, j + 3);// 取此字符串的末尾7位数的前3�?
		String tail = str.substring(j + 3, i);// 取此字符串的末尾7位数的后4�?
		// �?26位字母做成数组,你也可以添加�?些其它的可用字符
		String[] arr = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };

		String[] target = new String[3];
		// 将字母数组随机取3个字母组成一个字符串,一共组�?3个字符串放到目标数组target�?
		for (int s = 0; s < 3; s++) {
			Random random = new Random();
			int a = random.nextInt(arr.length);
			int b = random.nextInt(arr.length);
			int c = random.nextInt(arr.length);
			target[s] = arr[a] + arr[b] + arr[c];
		}
		return (target[0] + tail + target[2]);// �?3个字母段与两个数字段组合输出随机ID

	}

	/*
	 * 货币符号基本上为3位字母表示法,如人民币为CNY,港币为HKD,美元为USD;如 ¥�??$ 等�??
	 */
	public static String getCurrencySymbol(Context context, String currencyCode) {
		String currencySymbol = null;
		currencyCode = currencyCode.toUpperCase();// 货币代码只接受全大写
		Currency currency = Currency.getInstance(currencyCode);
		Locale locale = context.getResources().getConfiguration().locale;
		try {
			currencySymbol = currency.getSymbol(locale);
		} catch (IllegalArgumentException e) {
			currencySymbol = "�?";
			Log.e("StringUtils", "illegal currency code !");
		}
		return currencySymbol;
	}

	public static String getDateTime() {

		String mYear = "";
		String mMonth = "";
		String mDay = "";
		String mWay = "";

		Calendar c = Calendar.getInstance();
		c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
		mYear = String.valueOf(c.get(Calendar.YEAR)); // 获取当前年份
		mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
		mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号�?
		mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
		if ("1".equals(mWay)) {
			mWay = "一";
		} else if ("2".equals(mWay)) {
			mWay = "二";
		} else if ("3".equals(mWay)) {
			mWay = "三";
		} else if ("4".equals(mWay)) {
			mWay = "四";
		} else if ("5".equals(mWay)) {
			mWay = "五";
		} else if ("6".equals(mWay)) {
			mWay = "六";
		} else if ("7".equals(mWay)) {
			mWay = "日";
		}


		return mYear + "年," + "星期" + mWay + ":" + mMonth + "月" + mDay + "日";
	}

	public static String getDateTime(String text) {

		String mYear = "";
		String mMonth = "";
		String mDay = "";
		String mWay = "";

		Calendar c = Calendar.getInstance();
		c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
		mYear = String.valueOf(c.get(Calendar.YEAR)); // 获取当前年份
		mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
		mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号�?
		mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
		if ("1".equals(mWay)) {
			mWay = "一";
		} else if ("2".equals(mWay)) {
			mWay = "二";
		} else if ("3".equals(mWay)) {
			mWay = "三";
		} else if ("4".equals(mWay)) {
			mWay = "四";
		} else if ("5".equals(mWay)) {
			mWay = "五";
		} else if ("6".equals(mWay)) {
			mWay = "六";
		} else if ("7".equals(mWay)) {
			mWay = "日";
		}

		return "星期" + mWay + text + mYear + "年" + mMonth + "月" + mDay + "日";
	}

	public static String getDateTimes() {

		String mYear = "";
		String mMonth = "";
		String mDay = "";
		String mWay = "";

		Calendar c = Calendar.getInstance();
		c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
		mYear = String.valueOf(c.get(Calendar.YEAR)); // 获取当前年份
		mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
		mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号�?
		mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
		if ("1".equals(mWay)) {
			mWay = "一";
		} else if ("2".equals(mWay)) {
			mWay = "二";
		} else if ("3".equals(mWay)) {
			mWay = "三";
		} else if ("4".equals(mWay)) {
			mWay = "四";
		} else if ("5".equals(mWay)) {
			mWay = "五";
		} else if ("6".equals(mWay)) {
			mWay = "六";
		} else if ("7".equals(mWay)) {
			mWay = "日";
		}
		String str="";
		if (mMonth.length()<2) {
			str="0" + mMonth ;
		}else{
			str=mMonth;
		}
		
		return mYear + "-" +str + "-" + mDay + "  " + "星期" + mWay;
	}

	// 三个月后的时�?
	/**
	 * add lzz 2011.01.18 获取当前日期的后三个月日�?
	 * 
	 * @return 三个月后的时�?
	 */
	public static String getLastMonth() {
		Date date = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, 3);
		Date otherDate = cal.getTime();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Log.i(TAG, "today:   " + dateFormat.format(date)
				+ "   3   months   after:   " + dateFormat.format(otherDate));
		return dateFormat.format(otherDate);
	}

	// 判断手机格式是否正确
	public static boolean isMobileNO(String mobiles) {
		Pattern p = Pattern
				.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
		Matcher m = p.matcher(mobiles);

		return m.matches();
	}

	// 判断email格式是否正确
	public static boolean isEmail(String email) {
		String str = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
		Pattern p = Pattern.compile(str);
		Matcher m = p.matcher(email);

		return m.matches();
	}

	// 判断是否全是数字
	public static boolean isNumeric(String str) {
		Pattern pattern = Pattern.compile("[0-9]*");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches()) {
			return false;
		}
		return true;
	}

	// 随机生成四位
	public static String random(int n) {
		if (n < 1 || n > 10) {
			throw new IllegalArgumentException("cannot random " + n
					+ " bit number");
		}
		Random ran = new Random();
		if (n == 1) {
			return String.valueOf(ran.nextInt(10));
		}
		int bitField = 0;
		char[] chs = new char[n];
		for (int i = 0; i < n; i++) {
			while (true) {
				int k = ran.nextInt(10);
				if ((bitField & (1 << k)) == 0) {
					bitField |= 1 << k;
					chs[i] = (char) (k + '0');
					break;
				}
			}
		}
		return new String(chs);
	}

	// 得到今天和明天的日期
	public static String getDateForNowAndTomo(int day) {

		Date date = new Date();// 取时�?
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);

		calendar.add(calendar.DATE, day);// 把日期往后增加一�?.整数�?后推,负数�?前移�?
		date = calendar.getTime(); // 这个时间就是日期�?后推�?天的结果
		int number = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = formatter.format(date);

		System.out.println("取得的日期是�?" + dateString + weeks[number]);
		return dateString + ":" + number;
	}

	// 得到今天和明天的日期
	public static String getDateForNowAndTomos(int day) {

		Date date = new Date();// 取时�?
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);

		calendar.add(calendar.DATE, day);// 把日期往后增加一�?.整数�?后推,负数�?前移�?
		date = calendar.getTime(); // 这个时间就是日期�?后推�?天的结果
		int number = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = formatter.format(date);

		System.out.println("取得的日期是�?" + dateString + weeks[number]);
		return dateString + "" + weeks[number];
	}

	// 得到今天和明天的日期
	public static String getDateForNowAndTomos2(int day) {

		Date date = new Date();// 取时�?
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);

		calendar.add(calendar.DATE, day);// 把日期往后增加一�?.整数�?后推,负数�?前移�?
		date = calendar.getTime(); // 这个时间就是日期�?后推�?天的结果
		int number = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = formatter.format(date);

		System.out.println("取得的日期是�?" + dateString + weeks[number]);
		return dateString + " " + "星期" + weeks2[number];
	}

	// 得到今天和明天的日期
	public static String getDateTodayTommow(int day) {

		Date date = new Date();// 取时�?
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);

		calendar.add(calendar.DATE, day);// 把日期往后增加一�?.整数�?后推,负数�?前移�?
		date = calendar.getTime(); // 这个时间就是日期�?后推�?天的结果
		int number = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = formatter.format(date);

		System.out.println("取得的日期是�?" + dateString + weeks[number]);
		return "星期" + weeks2[number] + "  " + dateString;
	}

	// 得到今天和明天的日期
	public static String getDateTodayTommow3(int day) {

		Date date = new Date();// 取时�?
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);

		calendar.add(calendar.DATE, day);// 把日期往后增加一�?.整数�?后推,负数�?前移�?
		date = calendar.getTime(); // 这个时间就是日期�?后推�?天的结果
		int number = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		SimpleDateFormat formatter = new SimpleDateFormat("MM月dd�?");
		String dateString = formatter.format(date);

		System.out.println("取得的日期是�?" + dateString + weeks[number]);
		return dateString;
	}

	// 今天明天日期判断
	public static int compareDate(String DATE1, String DATE2) {

		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date dt1 = df.parse(DATE1);
			Date dt2 = df.parse(DATE2);
			if (dt1.getTime() > dt2.getTime()) {
				System.out.println("明天");
				return 1;
			} else if (dt1.getTime() < dt2.getTime()) {

				System.out.println("昨天");
				return 0;
			} else {
				System.out.println("今天");
				return 2;
			}

		} catch (Exception exception) {
			exception.printStackTrace();
			return -1;
		}

	}

	// 将日期格式化
	public static String formDateStr(String date) {

		SimpleDateFormat simpleDF = new SimpleDateFormat("yyyy年MM月dd�?");
		SimpleDateFormat simpleDF2 = new SimpleDateFormat("yyyy-MM-dd");
		String result = "";
		try {
			Date date1 = (Date) simpleDF.parseObject(date.toString());

			result = simpleDF2.format(date1);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return result;
	}

	// 将日期格式化
	public static String formDateStr2(String date) {

		SimpleDateFormat simpleDF = new SimpleDateFormat("yyyy�?,MM月dd�?");
		SimpleDateFormat simpleDF2 = new SimpleDateFormat("yyyy-MM-dd");
		String result = "";
		try {
			Date date1 = (Date) simpleDF.parseObject(date.toString());

			result = simpleDF2.format(date1);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return result;
	}

	// 今天和明�?
	public static String getTodayOrTomorrow(int num) {

		String mYear = "";
		String mMonth = "";
		String mDay = "";
		String mWay = "";

		Calendar c = Calendar.getInstance();
		c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
		mYear = String.valueOf(c.get(Calendar.YEAR)); // 获取当前年份
		mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
		mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH) + num);// 获取当前月份的日期号�?
		mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
		if ("1".equals(mWay)) {
			mWay = "一";
		} else if ("2".equals(mWay)) {
			mWay = "二";
		} else if ("3".equals(mWay)) {
			mWay = "三";
		} else if ("4".equals(mWay)) {
			mWay = "四";
		} else if ("5".equals(mWay)) {
			mWay = "五";
		} else if ("6".equals(mWay)) {
			mWay = "六";
		} else if ("7".equals(mWay)) {
			mWay = "日";
		}

		return mYear + "年," + "星期" + mWay + ":" + mMonth + "月" + mDay + "日";
	}

	public static int compareTime(String s, String e) {

		SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd");

		try {
			Date start = form.parse(s);

			Date end = form.parse(e);

			long i = end.getTime() - start.getTime();

			if (i > 0) {// 明天
				return -1;
			} else if (i == 0) {// 今天

				return 0;

			} else {// 昨天
				return 1;

			}
		} catch (Exception f) {
			f.printStackTrace();
			return -2;
		}

	}

	// 指定�?个日期判断在今天之前还是等于今天
	@SuppressLint("SimpleDateFormat")
	public static int compareDatetime(String date) {

		int flag = 0;
		SimpleDateFormat simpleDF2 = new SimpleDateFormat("yyyy-MM-dd");
		Date today = new Date();
		String newDate = simpleDF2.format(today);

		try {
			Date date1 = (Date) simpleDF2.parseObject(newDate.toString());// 今天

			Date date2 = (Date) simpleDF2.parseObject(date.toString());// 比较的日�?

			Log.i(TAG, "===当前日期===" + newDate);
			Log.i(TAG, "===要比较的日期===" + date);

			Log.i(TAG, "===要比较的日期===" + date1 + "------" + date2);

			if (date2.before(date1)) {// 明天
				flag = 0;
			} else if (date2.equals(date1)) {// 今天
				flag = 1;

			} else {// 昨天
				flag = -1;

			}
		} catch (Exception e) {
			Log.i(TAG, "比较日期异常", e);
			flag = -2;
			return flag;
		}

		return flag;
	}

	/**
	 * 比较 两个日期
	 */
	public static int compare_dates(String DATE1, String DATE2) {

		DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
		try {
			Date dt1 = df.parse(DATE1);
			Date dt2 = df.parse(DATE2);
			if (dt1.getTime() > dt2.getTime()) {

				return 1;
			} else if (dt1.getTime() < dt2.getTime()) {
				return -1;
			} else {
				return 0;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return 0;
	}

	public static int compare_dates2(String DATE1, String DATE2) {

		Log.i(TAG, DATE1 + "--ddfdfsfas33333---" + DATE2);

		DateFormat df = new SimpleDateFormat("yyyy年MM月dd�?");
		try {
			Date dt1 = df.parse(DATE1);
			Date dt2 = df.parse(DATE2);

			if (dt1.after(dt2)) {// 昨天

				return -1;
			} else if (dt1.before(dt2)) {// �?
				return 1;

			} else {// 今天
				return 0;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return 0;
	}

	/**
	 * 比较 两个日期
	 */
	public static int compare_dates3(String DATE1, String DATE2) {

		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date dt1 = df.parse(DATE1);
			Date dt2 = df.parse(DATE2);
			if (dt1.after(dt2)) {// 昨天

				return -1;
			} else if (dt1.before(dt2)) {// �?
				return 1;

			} else {// 今天
				return 0;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return 0;
	}

	/**
	 * 订单状�??
	 * 
	 * @param str
	 * @return
	 */
	public static String ParseOrderState(String str) {
		String result = "";
		switch (Integer.parseInt(str)) {
		case 00:
			result = "未付款";
			break;

		case 01:
			result = "已付款";
			break;

		case 02:
			result = "已支付";
			break;

		case 03:
			result = "退订需审核";
			break;

		case 04:
			result = "会计审核通过";
			break;

		case 05:
			result = "会计审核未通过";
			break;

		case 18:
			result = "订单审核通过";
			break;

		case 19:
			result = "前台审核未通过";
			break;

		case 20:
			result = "后台审核未通过";
			break;

		case 97:
			result = "订单需审核";
			break;
		case 95:
			result = "订单后台需审核";
		case 98:
			result = "作废订单";
		case 11:
			result = "已出票";
		}

		return result;

	}

	public static int compareDateBym(String str, int day) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		try {
			Date d1 = getDateAfter(new Date(), day);
			Date d2 = sdf.parse(str);

			if (d1.after(d2)) {
				return 1;
			} else if (d1.equals(d2)) {
				return 0;
			} else {
				return -1;
			}

		} catch (Exception e) {
			e.printStackTrace();
			return -2;
		}

	}

	/**
	 * 得到几天后的时间
	 */
	public static Date getDateAfter(Date d, int day) {
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
		return now.getTime();
	}

}