1、时间转时间戳

public static long strToTimestamp(String dateTimeStr) throws Exception {
Timestamp time = Timestamp.valueOf(dateTimeStr);
return time.getTime();
}

2、时间戳转时间

public static String timestampToStr(long timestamp) throws Exception {
Timestamp ts = new Timestamp(timestamp);
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(ts);
}

3、时间转换

    public static Map strTimeTomap(String dateTimeStr) throws Exception {
// Timestamp ts = new Timestamp(timestamp);
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat dsdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); Date date = sdf.parse(dateTimeStr);
cal.setTime(date);
String week_day = String.valueOf(cal.get(Calendar.DAY_OF_WEEK) - 1);
String hour = String.valueOf(cal.get(Calendar.HOUR_OF_DAY)); cal.set(Calendar.DAY_OF_MONTH, 1);
String first_day_month = dsdf.format(cal.getTime());
// System.out.println(first_day_month); cal.setTime(date);
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
String first_day_week = dsdf.format(cal.getTime());
// System.out.println(first_day_week); String day = dsdf.format(date);
Map map = new HashMap<String,String>();
map.put("hour",hour);
map.put("day",day);
map.put("week_day",week_day);
map.put("first_day_week",first_day_week);
map.put("first_day_month",first_day_month); return map;
} @Test
public void test() throws Exception {
System.out.println(strTimeTomap("2019-04-18 12:31:05"));
}

java时间处理,获取当前时间的小时,天,本周周几,本周周一的日期,本月一号的日期的更多相关文章

  1. Python获取当前时间_获取格式化时间_格式化日期

    Python获取当前时间_获取格式化时间: Python获取当前时间: 使用 time.time( ) 获取到距离1970年1月1日的秒数(浮点数),然后传递给 localtime 获取当前时间 #使 ...

  2. 【转】python 输入一个时间,获取这个时间的下一秒

    原文:https://blog.csdn.net/l_d_56/article/details/84832198 输入一个时间,获取这个时间的下一秒 PS:下面代码使用于 python 2.7 tim ...

  3. Java 获取各时区时间,获取当前时间到格林威治时间1970年01月01日00时00分00秒的秒数

    格林威治时间即UTC/GMT时间,1970年01月01日00时00分00秒(即UTC+8的北京时间1970年01月01日08时00分00秒)计算代码如下: /** * 获取指定时间到格林威治时间的秒数 ...

  4. js 的date的format时间,获取当前时间,前一天的日期

    Date.prototype.Format = function (fmt) { //author: meizz var o = { "M+": this.getMonth() + ...

  5. golang 时间戳 时间格式化 获取当前时间 timestamp 计算时间差

    获取当前时间 func Now func Now() Time 1 Now returns the current local time. func (Time) UTC func (t Time) ...

  6. C# 获取两个时间段之间的所有时间与获取当前时间所在的季度开始和结束时间

    一:C# 获取两个时间段之间的所有时间 public List<string> GetTimeList(string rq1, string rq2) { List<string&g ...

  7. MySQL时间函数-获取当前时间-时间差

    MySQL中获取当前时间为now(),不同于sqlserver getdate(). SQLServer转MySQL除变化top 1 -> limit 1之后报错: limit [Err] 15 ...

  8. redis基本操作,基于StringRedisTemplate,存储,取值,设置超时时间,获取超时时间,插入list操作

    @Autowired private StringRedisTemplate stringRedisTemplate; @GetMapping("/test") void test ...

  9. java中如何获取系统时间

    需要引入的包有: import java.util.Date; 此为获取当前系统时间,合适为“1991-01-01” String now = "";    SimpleDateF ...

随机推荐

  1. js重点--原型链继承详解

    上篇说过了关于原型链继承的问题,这篇详解一下. 1. function animals(){ this.type = "animals"; } animals.prototype. ...

  2. TF Multi-GPU single input queue

    多GPU的数据训练,feed images, labels = cifar10.distorted_inputs() split_images = tf.split(images, FLAGS.num ...

  3. [图解Java]ReentrantLock重入锁

    图解ReentrantLock 0. demo 我先给出一个demo, 这样大家就可以根据我给的这段代码, 边调试边看源码了. 还是那句话: 注意"My" , 我把Reentran ...

  4. python之路(8)常用模块

    目录 os模块 sys模块 json模块 pickle模块 xml模块 re模块 logging模块 configparser模块 hashlib模块 time模块 random模块 subproce ...

  5. 基于jeesite的cms系统(六):Lucene全文搜索引擎

    1.lucene初始化 // @Value("${lucene.index.path}") private String indexPath = "/Users/vito ...

  6. C# 01 Primitive Types and Expressions

    Class Data or Attributes state of the application Methods or Functions have behavior Namespace is a ...

  7. 原生JS实现简易评论更新功能

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. .NET尝试访问某方法失败

    今天发现了一个错误: xxxx.xxxx尝试访问xxxx.xxxx方法失败. 调试无果,经过分析后得到这是.NET引用的问题.果然有了这个方向后,发现了引用不匹配的问题,问题随之解决. 记录一下.

  9. day 20 - 1 序列化模块,模块的导入

    序列化模块 首先我们来看一个序列:'sdfs45sfsgerg4454287789sfsf&*0' 序列 —— 就是字符串序列化 —— 从数据类型 --> 字符串的过程反序列化 —— 从 ...

  10. spring cloud 集群健康监控--turbine-dashboard仪表盘

    这里仍然以Windows和jdk为运行环境,按照下面的步骤打包-运行-访问就能看到效果. 运维健康监控--hystrix-dashboard仪表盘 java -jar F:\jars-dashboar ...