时间格式转换(Date String)

/**
	 * 传入具体一天,返回具体日期减少一天
	 * 
	 * @throws ParseException
	 */
	public static String subDay(String date) {
		final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date dt = null;
		try {
			dt = sdf.parse(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		final Calendar rightNow = Calendar.getInstance();
		rightNow.setTime(dt);
		rightNow.add(Calendar.DATE, -1);
		final Date dt1 = rightNow.getTime();
		final String reStr = sdf.format(dt1);
		return reStr;

	}

	/**
	 * @param dateInMs
	 *            传入的时间long值
	 * @param days
	 *            天数 如 -1 +1
	 * @return
	 */
	public static String getDateByLong(long dateInMs, int days) {
		final long curDateInMs = dateInMs + days * 24 * 60 * 60 * 1000;
		return dateFormatter.format(new Date(curDateInMs));
	}

	/**
	 * 时间(yyyy-MM-dd HH:mm:ss):String格式转Date格式
	 */
	public static Date parseTimeFormattoDate(String date) {
		Date time = null;
		try {
			time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return time;
	}

	/**
	 * 时间(yyyy-MM-dd HH:mm:ss):Date格式转String格式
	 */
	public static String parseTimeToString(Date date) {
		final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(date);
	}

	/**
	 * 时间(yyyy-MM-dd):String格式转Date格式
	 */
	public static Date parseTimeFormattoDayDate(String date) {
		Date time = null;
		try {
			time = new SimpleDateFormat("yyyy-MM-dd").parse(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return time;
	}

	/**
	 * 时间(yyyy-MM-dd):Date格式转String格式
	 */
	public static String parseTimeFormattoDayDate(Date date) {
		String time = null;
		try {
			time = new SimpleDateFormat("yyyy-MM-dd").format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return time;
	}

	/**
	 * 时间(yyyy-MM):Date格式转String格式
	 */
	public static String parseTimeFormattoMonthDate(Date date) {
		String time = null;
		try {
			time = new SimpleDateFormat("yyyy-MM").format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return time;
	}

	/**
	 * 获取:昨天22:00 今天22:00
	 * 
	 * @return
	 */
	public static Map getDay() {
		final Map dayMap = new HashMap<>();
		final Date date = new Date();
		final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		final String format = df.format(date);
		final String todayDate = format + " 22:00:00";
		final Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, -1);
		final Date time = calendar.getTime();
		final String format2 = df.format(time);
		final String beforeDate = format2 + " 22:00:00";
		dayMap.put("todayDate", todayDate);
		dayMap.put("beforeDate", beforeDate);
		return dayMap;
	}

	/**
	 * 时间(yyyy-MM):String格式转Date格式
	 */
	public static Date parseTimeFormattoMonthDate(String date) {
		Date time = null;
		try {
			time = new SimpleDateFormat("yyyy-MM").parse(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return time;
	}

你可能感兴趣的:(java,基础)