博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java中Date的使用情况
阅读量:4693 次
发布时间:2019-06-09

本文共 1375 字,大约阅读时间需要 4 分钟。

在开发中常使用情况。

1、将String转为date  例如"201604131630"

//设置日期格式

public SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

public SimpleDateFormat sdfOrignal = new SimpleDateFormat("yyyyMMddHHmm");

String startTimeS = "201604131630";

long startTime = sdfOrignal.parse(startTimeS).getTime();

String stringStartTime = sdf.format(new Date(startTime))

 

2、将当前系统时间转为String

//设置日期格式

public SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

//new Date()为获取当前系统时间

String date = sf.format(new Date());

3、Calendar 的使用。

Calendar 设置数据测试:

//设置日期格式

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");

Calendar calendar = Calendar.getInstance();

 //测试数据为13日16:30数据
 calendar.set(Calendar.DAY_OF_MONTH, 13);
 calendar.set(Calendar.HOUR_OF_DAY, 16);
 calendar.set(Calendar.MINUTE, 30);
 calendar.set(Calendar.MILLISECOND, 0);
 long  lastTime = calendar.getTimeInMillis()/60000;
 String fmtTime = sdf.format(new Date(lastTime*60000))

Calendar 获取数据测试:

Calendar c = Calendar.getInstance();//可以对每个时间域单独修改

int year = c.get(Calendar.YEAR);

int month = c.get(Calendar.MONTH);
int date = c.get(Calendar.DATE);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
System.out.println(year + "/" + month + "/" + date + " " +hour + ":" +minute + ":" + second);

 

就写这么多吧。。。。

转载于:https://www.cnblogs.com/xubiao/p/5422217.html

你可能感兴趣的文章
(转)Excel的 OleDb 连接串的格式(连接Excel 2003-2013)
查看>>
Java并发编程
查看>>
Git Stash用法
查看>>
sql server 2008学习8 sql server存储和索引结构
查看>>
Jquery radio选中
查看>>
postgressql数据库中limit offset使用
查看>>
测试思想-集成测试 关于接口测试 Part 2
查看>>
windows下mysql密码忘了怎么办?【转】
查看>>
php生成器使用总结
查看>>
T-SQL中的indexof函数
查看>>
javascript基础之数组(Array)对象
查看>>
mysql DML DDL DCL
查看>>
RAMPS1.4 3d打印控制板接线与测试1
查看>>
python with语句中的变量有作用域吗?
查看>>
24@Servlet_day03
查看>>
初级ant的学习
查看>>
redis数据结构--String
查看>>
memcached 细究(三)
查看>>
使用svn——项目的目录布局
查看>>
RSA System.Security.Cryptography.CryptographicException
查看>>