时间格式转换(Date String)

本文详细介绍了如何在不同的编程环境中将日期从一种格式转换为另一种格式,涵盖了JavaScript、Python和Java等常见语言的日期处理方法,包括使用内置库和自定义函数进行日期字符串的解析和格式化。无论你是新手还是经验丰富的开发者,都将从中获益,更好地理解和操作日期格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
	 * 传入具体一天,返回具体日期减少一天
	 * 
	 * @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<String, String> getDay() {
		final Map<String, String> 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;
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值