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(); ...
随机推荐
- MongoDB 基础命令行
本文专门介绍MongoDB的命令行操作.其实,这些操作在MongoDB官网提供的Quick Reference上都有,但是英文的,为了方便,这里将其稍微整理下,方便查阅. 登录和退出 mongo命令直 ...
- Linux - Shell脚本调试方法
Shell脚本调试选项 Shell本身提供一些调试方法选项: -n,读一遍脚本中的命令但不执行,用于检查脚本中的语法错误. -v,一边执行脚本,一边将执行过的脚本命令打印到标准输出. -x,提供跟踪执 ...
- Shell基础整理
Shell的作用是将用户输入的文本命令转换成内核能识别的数据指令交给内核进行执行,内核需要翻译成二进制交由CPU底层来执行 用户层->Shell->调用对应应用程序->ke ...
- Microsoft OneScript 团队发布的最新一版在 SQL Server Management Studio 中运行的脚本,可以帮助我们获取更详细的版本信息。
该脚本有以下几点: 1. SQL Server 的主要版本.服务级别和版本类别 2. 已安装SP包.累计更新CU,历史更新的QFE\ GDR 3. 推荐当前SP包可以安装最新的CU,并给到相关资源地地 ...
- SQL实现分组查询取前几条记录
我要实现的功能是统计订单日志表中每一个订单的前三条日志记录,表结构如下: 一个订单在定点杆日志表中有多条记录,要根据时间查询出每一个订单的前三条日志记录,sql如下: select b.OrderNu ...
- springMVC中Dispatcher中的/和/*的区别
1. 首先 / 这个是表示默认的路径,及表示:当没有找到可以匹配的URL就用这个URL去匹配.2. 在springmvc中可以配置多个DispatcherServlet,比如: 配置多个Dispatc ...
- 准备.Net转前端开发-WPF界面框架那些事,搭建基础框架
题外话 最近都没怎么写博客,主要是最近在看WPF方面的书<wpf-4-unleashed.pdf>,挑了比较重要的几个章节学习了下WPF基础技术.另外,也把这本书推荐给目前正在从事WPF开 ...
- JavaScript一些函数
1.prompt()函数:有两个参数,一个是显示用户输入框上面的标签,另一个是输入框的初始值.用来接收用户输入的值,然后把它返回到代码中: 例如: <doctype html> <h ...
- 基于Lumisoft.NET组件的SMTP账号登陆检测
在邮件处理的方面,Lumisoft.NET可以说是非常不错的一个选择,我在前面几篇文章中都介绍过这个组件. 基于Lumisoft.NET组件开发碰到乱码等一些问题的解决 基于Lumisoft.NET组 ...
- Docker on CentOS for beginners
Introduction The article will introduce Docker on CentOS. Key concepts Docker Docker is the world's ...