java获取时间戳的方法
JAVA 获取当前月的初始时间的时间戳
- public static long getMonthFirstDay() {
- Calendar calendar = Calendar.getInstance();// 获取当前日期
- calendar.add(Calendar.MONTH, 0);
- calendar.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
- calendar.set(Calendar.HOUR_OF_DAY, 0);
- calendar.set(Calendar.MINUTE, 0);
- calendar.set(Calendar.SECOND, 0);
- System.out.println(calendar.getTimeInMillis());
- return calendar.getTimeInMillis();
- }
获取当前时间戳
//方法 一
System.currentTimeMillis();
//方法 二
Calendar.getInstance().getTimeInMillis();
//方法 三
new Date().getTime();
Calendar.getInstance().getTimeInMillis() 这种方式速度最慢,这是因为Canlendar要处理时区问题会耗费较多的时间。
在开发过程中,通常很多人都习惯使用new Date()来获取当前时间,使用起来也比较方便,同时还可以获取与当前时间有关的各方面信息,例如获取小时,分钟等等,而且还可以格式化输出,包含的信息是比较丰富的。但是有些时候或许你并不需要获取那么多信息,你只需要关心它返回的毫秒数就行了,例如getTime()。为了获取这个时间戳,很多人也喜欢使用new Date().getTime()去获取,咋一看没什么问题,但其实没这个必要。
其实看一下java的源码就知道了:
public Date()
{
this(System.currentTimeMillis());
}
已经很明显了,new Date()所做的事情其实就是调用了System.currentTimeMillis()。如果仅仅是需要或者毫秒数,那么完全可以使用System.currentTimeMillis()去代替new Date(),效率上会高一点。况且很多人喜欢在同一个方法里面多次使用new Date(),通常性能就是这样一点一点地消耗掉,这里其实可以声明一个引用。
获取当前时间
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
String date = df.format(new Date());// new Date()为获取当前系统时间,也可使用当前时间戳
我们看一下JAVA获得当前时间的几种方法
package com.xjp.common.util;
import java.sql.Timestamp;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.ttsoft.framework.util.DateUtil;
/**
* Title: 时间获取
* Description: 当前时间
* Company:
* @author jiq
* @version 1.0
*/
public class XJPDateUtil extends DateUtil {
public static final String[] months = { "一月", "二月", "三月", "四月", "五月", "六月",
"七月", "八月", "九月", "十月", "十一月", "十二月", };
public static final String[] quarters = { "一季度", "二季度", "三季度", "四季度" };
public XJPDateUtil() {
}
/**
* 获取日期字符串。
*
*
* 日期字符串格式: yyyyMMdd
* 其中:
* yyyy 表示4位年。
* MM 表示2位月。
* dd 表示2位日。
*
*
* @return String "yyyyMMdd"格式的日期字符串。
*/
public static String getDate() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
return formatter.format(new Date());
}
/**
* 获取当前年度字符串。
*
*
* 日期字符串格式: yyyy
* 其中:
* yyyy 表示4位年。
*
*
* @return String "yyyy"格式的当前年度字符串。
*/
public static String getNowYear() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
return formatter.format(new Date());
}
/**
* 获取当前月度字符串。
*
*
* 日期字符串格式: MM
* 其中:
* MM 表示4位年。
*
*
* @return String "yyyy"格式的当前月度字符串。
*/
public static String getNowMonth() {
SimpleDateFormat formatter = new SimpleDateFormat("MM");
return formatter.format(new Date());
}
/**
* 获取当前月度字符串。
*
*
* 日期字符串格式: dd
* 其中:
* dd 表示4位年。
*
*
* @return String "yyyy"格式的当前月度字符串。
*/
public static String getNowDay() {
SimpleDateFormat formatter = new SimpleDateFormat("dd");
return formatter.format(new Date());
}
/**
* 获取日期字符串。
*
*
* 日期字符串格式: yyyyMMdd
* 其中:
* yyyy 表示4位年。
* MM 表示2位月。
* dd 表示2位日。
*
*
* @param date
* 需要转化的日期。
* @return String "yyyyMMdd"格式的日期字符串。
*/
public static String getDate(Date date) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
return formatter.format(date);
}
/**
* 获取日期字符串。
*
*
* 日期字符串格式: yyyyMMdd
* 其中:
* yyyy 表示4位年。
* MM 表示2位月。
* dd 表示2位日。
*
*
* @param date
* 需要转化的日期。
* @return String "yyyyMMdd"格式的日期字符串。
*/
/**
* 将指定的日期字符串转化为日期对象
*
* @param dateStr
* 日期字符串
* @return java.util.Date
* @roseuid 3F39FE450385
*/
public static Date getDate(String dateStr) {
if (XJPTypeChecker.isDate(dateStr)) { // 日期型
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
try {
java.util.Date date = df.parse(dateStr);
return date;
} catch (Exception ex) {
Logger.write("日期格式不符合或者日期为空!请检查!");
return null;
} // end try - catch
} else if (XJPTypeChecker.isDatetime(dateStr)) { // 日期时间型
SimpleDateFormat df = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss.SSS");
try {
java.util.Date date = df.parse(dateStr);
return date;
} catch (Exception ex) {
return null;
} // end try - catch
} // end if
return null;
}
/**
* 获取日期字符串。
*
*
* 日期字符串格式: yyyy-MM-dd
* 其中:
* yyyy 表示4位年。
* MM 表示2位月。
* dd 表示2位日。
*
*
* @return String "yyyy-MM-dd"格式的日期字符串。
*/
public static String getHyphenDate() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
return formatter.format(new Date());
}
/**
* 获取日期字符串。
*
*
* 日期字符串格式: yyyy-MM-dd
* 其中:
* yyyy 表示4位年。
* MM 表示2位月。
* dd 表示2位日。
*
*
* @param date
* 需要转化的日期。
* @return String "yyyy-MM-dd"格式的日期字符串。
*/
public static String getHyphenDate(Date date) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
return formatter.format(date);
}
/**
* 将"yyyyMMdd"格式的日期字符串转换为日期对象。
*
* @param source
* 日期字符串。
* @return Date 日期对象。
*/
public static Date parsePlainDate(String source) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
return sdf.parse(source, new ParsePosition(0));
}
/**
* 将“yyyy-MM-dd”格式的日期字符串转换为日期对象。
*
* @param source
* 日期字符串。
* @return Date 日期对象。
*/
public static Date parseHyphenDate(String source) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.parse(source, new ParsePosition(0));
}
/**
* 将指定格式的日期字符串转换为日期对象。
*
* @param source
* 日期字符串。
* @param pattern
* 模式。
* @return Date 日期对象。
*/
public static Date parseDate(String source, String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.parse(source, new ParsePosition(0));
}
/**
* 将“yyyy-MM-dd”格式的日期字符串转换为“yyyyMMdd”格式的日期字符串。
*
* @param source
* 日期字符串。
* @return String "yyyymmdd"格式的日期字符串。
*/
public static String toPlainDate(String source) {
Date date = parseHyphenDate(source);
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
return formatter.format(date);
}
/**
* 将“yyyyMMdd”格式的日期字符串转换为“yyyy-MM-dd”格式的日期字符串。
*
* @param source
* 日期字符串。
* @return String "yyyy-MM-dd"格式的日期字符串。
*/
public static String toHyphenDate(String source) {
Date date = parsePlainDate(source);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
return formatter.format(date);
}
/**
* 获取时间戳,将日期对象转换为时间戳类型。
*
* @param date
* 日期对象
* @return Timestamp 时间戳
*/
public static Timestamp getTimestamp(Date date) {
return new Timestamp(date.getTime());
}
/**
* 获取时间戳,将当前日期转换为时间戳类型。
*
* @return Timestamp 时间戳
*/
public static Timestamp getTimestamp() {
return new Timestamp(new Date().getTime());
}
/**
* 将“yyyyMMdd”格式的日期字符串转换为Timestamp类型的对象。
*
* @param source
* 日期字符串
* @return Timestamp 时间戳
*/
public static Timestamp parseTimestamp(String source) {
Date date = parsePlainDate(source);
return getTimestamp(date);
}
/**
* 获得年度周期
* Example:
* XJPDateUtil.getPeriodYear("20040229" , -1);
* XJPDateUtil.getPeriodYear("20040228" , -1);
* XJPDateUtil.getPeriodYear("20020230" , 2);
*
* @param source
* 时间串
* @param yearPeriod
* 年度周期 -1代表本时间的上一年度,以次类推。
* @return String 时间。
*/
public static String getPeriodYear(String source, int yearPeriod) {
int p = Integer.parseInt(source.substring(0, 4)) + yearPeriod;
String newYear = String.valueOf(p)
+ source.substring(4, source.length());
Date date = parsePlainDate(newYear);
String s = getDate(date);
int ny = Integer.parseInt(s);
int sy = Integer.parseInt(newYear);
while (ny > sy) {
sy--;
ny = Integer.parseInt(getDate(parsePlainDate(String.valueOf(sy))));
}
return String.valueOf(sy);
}
/**
* 获取当前日期和时间
*
* @return String
*/
public static String getCurrentDateStr() {
Date date = new Date();
String str = null;
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
str = df.format(date);
return str;
}
/**
* 日期相加
*
* @param day
* 天数
* @return 返回相加后的日期
*/
public static String addDate(int day) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis() + ((long) day) * 24 * 3600
* 1000);
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
return df.format(c.getTime());
}
/**
* 返回毫秒
*
* @param date
* 日期
* @return 返回毫秒
*/
public static long getMillis(java.util.Date date) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.getTimeInMillis();
}
/**
* 获取当前日期和时间
* @param format 日期格式 例:yyyy-MM-dd hh:mm
* @return String
*/
public static String getNowDate(String format) {
Date date = new Date();
String str = null;
SimpleDateFormat df = new SimpleDateFormat(format);
str = df.format(date);
return str;
}
/**
* 将strmon的日期减小一个月
* @param mon
* @return
*/
public static String getReduceMonDate(String strmon) {
if (strmon != null && !strmon.equals("")) {
Date mon = parseHyphenDate(strmon);
mon.setMonth(mon.getMonth() - 1);
return getHyphenDate(mon);
} else {
return "";
}
}
public static String getTimeStr(String dateStr){
Date date=getDate(dateStr);
String str = null;
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
str = df.format(date);
return str;
}
public static String getTimeStr(){
String str="";
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
str = df.format(new Date());
return str;
}
}
java获取时间戳的方法的更多相关文章
- C#获取时间戳的方法
获取时间戳的方法 /// <summary> /// 获取时间戳 /// </summary> /// <param name= ...
- java 获取时间戳的三种方式
java 获取时间戳的三种方式 CreationTime--2018年7月13日16点29分 Author:Marydon 1.实现方式 方式一:推荐使用 System.currentTimeMi ...
- java获取文件大小的方法
目前Java获取文件大小的方法有两种: 1.通过file的length()方法获取: 2.通过流式方法获取: 通过流式方法又有两种,分别是旧的java.io.*中FileInputStream的ava ...
- java 获取时间戳
//java 获取时间戳 long currentTime=System.currentTimeMillis();
- java获取调用当前方法的方法名和行数
java获取调用当前方法的方法名和行数String className = Thread.currentThread().getStackTrace()[2].getClassName();//调用的 ...
- javascript获取时间戳的方法
javascript获取时间戳的方法<pre> START = new Date().getTime();</pre>这个是毫秒 除以1000就是秒啦
- Java 获取当前路径的方法总结
Java 获取当前路径的方法总结 1.利用System.getProperty()函数获取当前路径: System.out.println(System.getProperty("user. ...
- postman 获取时间戳的方法 和md5加密的方法
获取时间戳方法: postman.setGlobalVariable("timestamp",Math.round(new Date().getTime())); 这整句是获取 ...
- JAVA获取时间戳,哪个更快
目前获取毫秒值大概有下面三种方法 //方法 一 System.currentTimeMillis(); //方法 二 Calendar.getInstance().getTimeInMillis(); ...
随机推荐
- NopCommerce之任务执行
NOP任务提供两种:手动执行(立即)和定时执行两种. 首先来说下手动任务执行过程,下图是NOP定时任务管理界面: 从上面可以看出,我们可以选择具体的任务来手动执行任务(立即执行),当点击[立即执行]按 ...
- [OpenCV] Image Processing - Spatial Filtering
"利用给定像素周围的像素的值决定此像素的最终的输出值“ 教学效果: 策略: 1. 拉普拉斯,突出小细节: . 梯度,突出边缘: . 平滑过的梯度图像用于掩蔽: . 灰度变换,增加灰度动态范围 ...
- Unity 3D 一个简单的角色控制脚本
之所以写这个脚本,是因为我想起了我还是新手的时候,那时为了一个角色控制脚本百度了半天还是一无所获,因为看不懂啊,都写的太高级了 希望这个脚本能够帮助那些 像曾经的我一样迷失于代码中的新手们能够清晰的理 ...
- [转载]SharePoint 2013搜索学习笔记之自定义结果源
搜索中心新建好之后在搜索结果页上会默认有所有内容,人员,对话,视频这四个结果分类,每个分类会返回指定范围的搜索结果,这里我再添加了部门日志结果分类,搜索这个分类只会返回部门日志内容类型的搜索结果,要实 ...
- Exchange Server简介与搭建
一.Exchange Server简介Exchange Server 是微软公司的一套电子邮件服务组件,是个消息与协作系统. 简单而言,Exchange server可以被用来构架应用于企业.学校的邮 ...
- 就这样获取文件的MD5和大小
纠结真蛋疼 判断一件事值不值得去做的唯一标准是这件事是不是令我纠结.如果纠结了,就不去做了!但是,人总要活着,又能怎样.谁说男人就没有那么几天...... 从极速妙传说起 在现在各大厂商都推出免费云盘 ...
- Android、iOS和Windows Phone中的推送技术
推送并不是什么新技术,这种技术在互联网时代就已经很流行了.只是随着进入移动互联网时代,推送技术显得更加重要.因为在智能手机中,推送从某种程度上,可以取代使用多年的短信,而且与短信相比,还可以向用户展示 ...
- Sprint2演示分
团队贡献分: 朱杰:22 蔡京航:21 华子仪:20 甄增文:17
- 转载---CSS3实现曲线阴影和翘边阴影
预备知识 DIV+CSS基础 圆角:border-radius 2D变换:transform:skew && rotate 伪类::before 和 :after 代码 HTML结构代 ...
- DOM中 property 和 attribute 详解
被问到 property 和 attribute 的区别,想来也是要好好看一下. 一.基本概念区别 其实Attribute和Property这两个单词,翻译出来都是“属性”,<js高级程序设计& ...