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

连云港专业网站制作重庆seo报价

连云港专业网站制作,重庆seo报价,公司网站制作北京那家公司好,信息服务类网站怎么做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.tj-hxxt.cn/news/19516.html

相关文章:

  • 做ppt一般在什么网站好2023知名品牌营销案例100例
  • 淄博网站制作设计杭州做网站的公司排行
  • 公司登记兰州seo关键词优化
  • 域名备案和网站备案的区别seo关键词优化推广哪家好
  • 如何建立网站模板全国疫情最新情况公布
  • 百合视频做爰视频网站品牌运营具体做什么
  • java开发网站南京百度快速排名优化
  • 成都疫情越来越严重了贵州二级站seo整站优化排名
  • 网站建设与管理李洪心第三方关键词优化排名
  • 网站ui设计是什么陕西seo关键词优化外包
  • seo信息编辑招聘长沙网站优化推广方案
  • 深圳做网站找哪家好比百度还强大的搜索引擎
  • 网站图标代码百度搜一下
  • 做暧暖网站百度在线识图
  • 楼盘网站开发站群seo技巧
  • 网站开发设计文档企业网站的域名是该企业的
  • 什么app做网站收录网站排名
  • 四川微信网站建设推网络营销在哪里学比较靠谱
  • 外贸自建站 源码企业网络搭建方案
  • 如何做deal网站推广个人优秀网页设计
  • 小企业网站维护什么东西重庆seo整站优化方案范文
  • 网站建设教程 迅雷下载sem是什么方法
  • sae网站备案自制网站教程
  • 网站做qq链接代码每天三分钟新闻天下事
  • 建设部网站监理变更seo是什么公司
  • 一个购物网站开发语言微信做单30元一单
  • 做电影网站会不会涉及版权问题网络平台推广是干什么
  • zepto网站开发推广网站都有哪些
  • 多语言商城源码百度seo公司
  • 视频在线制作网站营销网店推广的软文