当前位置: 首页 > news >正文

pc网站建设费用北京企业网站建设制作

pc网站建设费用,北京企业网站建设制作,新乡网站建设找哪家,图书馆建设网站注意点0 背景 我们有时要获取时间,年月日时分秒周几,有时要以特定的格式出现。这时就要借助 SimpleDateFormat 或者 DateTimeFormatter。有时要某个月份有多少天需要借助 Calendar。所以有必要了解一些知识。 1 SimpleDateFormat simpledateFormat 线程不安全…

0 背景

  我们有时要获取时间,年月日时分秒周几,有时要以特定的格式出现。这时就要借助 SimpleDateFormat 或者 DateTimeFormatter。有时要某个月份有多少天需要借助 Calendar。所以有必要了解一些知识。

1 SimpleDateFormat

simpledateFormat 线程不安全,DateTimeFormatter 线程安全。

// 用法
String string = new SimpleDateFormat(String PATTERN, Locale locale).format(Date date);

  PATTERN 的样式有很多。具体可以看源码。

public static final String STAND_TIME = "yyyy-MM-dd HH:mm:ss";
public static final String FULL_TIME = "yyyy-MM-dd HH:mm:ss.SSS";
public static final String YEAR_MONTH_DAY = "yyyy-MM-dd";
public static final String YEAR_MONTH_DAY_CN = "yyyy年MM月dd日";
public static final String HOUR_MINUTE_SECOND = "HH:mm:ss";
public static final String HOUR_MINUTE_SECOND_CN = "HH时mm分ss秒";
public static final String YEAR = "yyyy";
public static final String MONTH = "MM";
public static final String DAY = "dd";
public static final String WEEK = "E";
public static final String HOUR = "HH";
public static final String MINUTE = "mm";
public static final String SECOND = "ss";
public static final String MILLISECOND = "SSS";

在这里插入图片描述

2 Calendar

Calendar calendar = Calendar.getInstance();
// 设置一个日历
calendar.set(Calendar.DATE,int);
calendar.set(Calendar.MONTH,int);
calendar.set(Calendar.YEAR,int);
// 年月日的增加,可正可负
calendar.add(Calendar.DATE,int);
calendar.add(Calendar.MONTH,int);
calendar.add(Calendar.YEAR,int);
// 年月日的回滚,不会影响大字段。如增加日,不会影响月,31 -> 1,不改变月份。
calendar.roll(Calendar.DATE,int);
calendar.roll(Calendar.MONTH,int);
calendar.roll(Calendar.YEAR,int);

  另外注意的参数

// week 是从星期天开始算的 1 - > 7
int a = calandar.get(Calendar.DAY_OF_WEEK);
// month 是从 0 开始的, 0 -> 11
int month = calendar.get(Calendar.MONTH);

3 时间工具 DateUtil

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.Objects;public class DateUtil {public static final String STAND_TIME = "yyyy-MM-dd HH:mm:ss";public static final String FULL_TIME = "yyyy-MM-dd HH:mm:ss.SSS";public static final String YEAR_MONTH_DAY = "yyyy-MM-dd";public static final String YEAR_MONTH_DAY_CN = "yyyy年MM月dd日";public static final String HOUR_MINUTE_SECOND = "HH:mm:ss";public static final String HOUR_MINUTE_SECOND_CN = "HH时mm分ss秒";public static final String YEAR = "yyyy";public static final String MONTH = "MM";public static final String DAY = "dd";public static final String WEEK = "E";public static final String HOUR = "HH";public static final String MINUTE = "mm";public static final String SECOND = "ss";public static final String MILLISECOND = "SSS";public static final String YESTERDAY = "昨天";public static final String TODAY = "今天";public static final String TOMORROW = "明天";/*** 获得当前时间** @return 例如 2023-09-29 10:00:00*/public static String getCurrentDateTime() {return new SimpleDateFormat(STAND_TIME, Locale.CHINESE).format(new Date());}/*** 获得当前完整时间** @return 例如 2023-09-29 10:00:00.123*/public static String getCurrentFullDateTime() {return new SimpleDateFormat(FULL_TIME, Locale.CHINESE).format(new Date());}/*** 获得今天年月日** @return 例如 2023-09-29*/public static String getCurrentYearMonthDay() {return new SimpleDateFormat(YEAR_MONTH_DAY, Locale.CHINESE).format(new Date());}/*** 获得年月日 中文版** @return 例如 2023年9月29日*/public static String getCurrentYearMonthDayCn() {return new SimpleDateFormat(YEAR_MONTH_DAY_CN, Locale.CHINESE).format(new Date());}/*** 获得年月日 自定义分隔符** @param delimiter 分隔符* @return 例如 2023/9/29*/public static String getCurrentYearMonthDayDelimiter(CharSequence delimiter) {return new SimpleDateFormat(YEAR + delimiter + MONTH + delimiter + DAY, Locale.CHINESE).format(new Date());}/*** 获得时分秒** @return 例如 10:00:00*/public static String getCurrentHourMinuteSecond() {return new SimpleDateFormat(HOUR_MINUTE_SECOND, Locale.CHINESE).format(new Date());}/*** 获得时分秒 中文版** @return 例如 10时00分00秒*/public static String getCurrentHourMinuteSecondCn() {return new SimpleDateFormat(HOUR_MINUTE_SECOND_CN, Locale.CHINESE).format(new Date());}/*** 获取时分秒 分隔符** @param delimiter 分隔符* @return 例如 2021/07/01*/public static String getCurrentHourMinuteSecondDelimiter(CharSequence delimiter) {return new SimpleDateFormat(HOUR + delimiter + MINUTE + delimiter + SECOND, Locale.CHINESE).format(new Date());}/*** 获得年** @return 例如 2023*/public static String getCurrentYear() {return new SimpleDateFormat(YEAR, Locale.CHINESE).format(new Date());}/*** 获得月** @return 例如 09*/public static String getCurrentMonth() {return new SimpleDateFormat(MONTH, Locale.CHINESE).format(new Date());}/*** 获得日** @return 29*/public static String getCurrentDay() {return new SimpleDateFormat(DAY, Locale.CHINESE).format(new Date());}/*** 获得时** @return 例如 10*/public static String getCurrentHour() {return new SimpleDateFormat(HOUR, Locale.CHINESE).format(new Date());}/*** 获得时分秒** @return 例如 00*/public static String getCurrentMinute() {return new SimpleDateFormat(MINUTE, Locale.CHINESE).format(new Date());}/*** 获得秒** @return 例如 00*/public static String getCurrentSecond() {return new SimpleDateFormat(SECOND, Locale.CHINESE).format(new Date());}/*** 获得毫秒** @return 例如 123*/public static String getCurrentMillisecond() {return new SimpleDateFormat(MILLISECOND, Locale.CHINESE).format(new Date());}/*** 获得当前时间戳** @return 例如 2023-9-29 10:00:00   为1695952800*/public static long getCurrentTimestamp() {return System.currentTimeMillis();}/*** 将时间转换成时间戳** @param time 时间* @return 返回时间戳 long*/public static long dateToStamp(String time) {SimpleDateFormat simpleDateFormat = new SimpleDateFormat(STAND_TIME, Locale.CHINESE);Date date = null;try {date = simpleDateFormat.parse(time);} catch (Exception e) {e.printStackTrace();}return Objects.requireNonNull(date).getTime();}/*** 将时间戳转换成时间** @param stamp 时间戳* @return 例如 2023-9-29 10:00:00*/public static String stampToDate(long stamp) {return new SimpleDateFormat(STAND_TIME, Locale.CHINESE).format(stamp);}/*** 返回今天是星期几** @return 例如 周五*/public static String getCurrentWeek() {return new SimpleDateFormat(WEEK, Locale.CHINESE).format(new Date());}/*** @param dateTime 日期 例如 2023-09-29* @return 例如 周五*/public static String getWeekOf(String dateTime) {Date date;if ("".equals(dateTime)) {date = new Date();} else {SimpleDateFormat sdf = new SimpleDateFormat(YEAR_MONTH_DAY, Locale.CHINESE);try {date = sdf.parse(dateTime);} catch (Exception e) {date = new Date();e.printStackTrace();}}return new SimpleDateFormat(WEEK,Locale.CHINESE).format(date);}/*** @param dateTime 日期 例如 2023-09-29* @return 例如 2023-09-28*/public static String getYesterdayOf(String dateTime) {SimpleDateFormat sdf = new SimpleDateFormat(YEAR_MONTH_DAY, Locale.CHINESE);Date date;try {date = sdf.parse(dateTime);} catch (ParseException e) {date = null;e.printStackTrace();}Calendar calendar = new GregorianCalendar();if (date != null) {calendar.setTime(date);}calendar.add(Calendar.DATE, -1);date = calendar.getTime();return new SimpleDateFormat(YEAR_MONTH_DAY, Locale.CHINESE).format(date);}/*** 获取输入日期的明天** @param dateTime 例如 2023-09-29* @return 例如 2023-09-30*/public static String getTomorrowOf(String dateTime) {SimpleDateFormat sdf = new SimpleDateFormat(YEAR_MONTH_DAY, Locale.CHINESE);Date date;try {date = sdf.parse(dateTime);} catch (ParseException e) {date = null;e.printStackTrace();}Calendar calendar = new GregorianCalendar();if (date != null) {calendar.setTime(date);}calendar.add(Calendar.DATE, +1);date = calendar.getTime();return new SimpleDateFormat(YEAR_MONTH_DAY, Locale.CHINESE).format(date);}/*** @param dateTime 一个时间 例如 2023-9-29* @return 相对今天而言是什么,比如今天是2023-9-30,返回昨天。*/public static String getDayInfoOf(String dateTime) {String dayInfo;String toDay = getCurrentYearMonthDay();String yesterday = getYesterdayOf(toDay);String tomorrow = getTomorrowOf(toDay);if (dateTime.equals(yesterday)) {dayInfo = YESTERDAY;} else if (dateTime.equals(toDay)) {dayInfo = TODAY;} else if (dateTime.equals(tomorrow)) {dayInfo = TOMORROW;} else {dayInfo = getWeekOf(dateTime);}return dayInfo;}/*** 返回当前月份的天数** @return 例如 9月份 返回30*/public static int getCurrentDaysOfMonth() {Calendar calendar = new GregorianCalendar();//把日期设置为当月第一天calendar.set(Calendar.DATE, 1);//日期回滚一天,也就是最后一天,roll 方法不更改大字段。calendar.roll(Calendar.DATE, -1);return calendar.get(Calendar.DATE);}/*** 返回指定年份月份的天数** @param year  年份 例如 2023* @param month 月份 例如 09* @return 例如 30*/public static int getDaysOfMothOf(int year, int month) {Calendar calendar = new GregorianCalendar();calendar.set(Calendar.YEAR, year);calendar.set(Calendar.MONTH, month - 1);//把日期设置为当月第一天calendar.set(Calendar.DATE, 1);//日期回滚一天,也就是最后一天,roll 方法不更改大字段。calendar.roll(Calendar.DATE, -1);return calendar.get(Calendar.DATE);}}
http://www.yayakq.cn/news/983722/

相关文章:

  • 网站建设教学方法探究wordpress 怎么加入插件
  • 济南网站建设开发与制作网站服务器做缓存
  • 网站的后台管理wordpress 注册连接
  • 益阳营销网站建设企业网站建设合同模板
  • 装饰公司营销型网站设计电子商务网站建设分析
  • 湖北网站郑州网站建设的软件
  • 西安网站建设哪个好天眼查企业入口免费
  • 科技网站有哪些大型公司网站建设目标
  • 学院网站设计方案吴江区经济开发区建设工程网站
  • 网站中的ppt链接怎么做搜索引擎网站
  • 载网站源码 怎么下载不了免费网站空间女人
  • 网站建设计划 文库最新网游网络游戏手游
  • 建设网站培训学校公司网址怎么做出来的
  • 网站建站网站实现用户登录
  • 建筑工人招聘网站怎么做网站前端代码有哪些问题
  • 怎么用程序做网站上海外贸网站优化
  • 西宁做网站君博领衔怎么黑进网站后台
  • 专注做一家男人最爱的网站上海源码网站建设公司
  • 网站上微信支付功能WordPress相册插件pro
  • 中华智能自建代理网站模板价格多少钱一张
  • 电影网站模板技能培训网站
  • 网站建设有哪些软件有哪些微商代理
  • 济南做网站找大标wordpress 做毕业设计
  • wordpress 360权重张家界seo网站优化
  • o2o网站建设案例wordpress标签分级
  • 房地产网站建设平台做交互网站
  • 数字博物馆网站建设网络运营商哪家好
  • 建网站公司 蓝纤科技媒介盒子
  • [8dvd]flash网站源文件 flash整站源码哈尔滨seo
  • 云南工程建设总承包公司网站搜索关键词查询