Java获取时间戳精确到年月日时

Java获取时间戳精确到年月日时,第1张

其实系统默认的都是毫秒数的时间戳, 所以你想要的2017-01-16 17:00:00 不是提取的, 而是格式化的

new SimpleDateFormat("yyyy-MM-dd HH:00:00")format(SystemcurrentTimeMillis());

第一种方法

package cnywtest;

import javatextSimpleDateFormat;

import javautilDate;

public class DateTest {

public static void main(String[] args){

Date today = new Date();

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

formatformat(today);

/输入日期/

Systemoutprintln(formatformat(today));

String datetime = formatformat(today);

//输入年

String year = datetimesubstring(0, datetimeindexOf("-"));

Systemoutprintln(year);

//输出月

String month = datetimesubstring(datetimeindexOf("-")+1, datetimelastIndexOf("-"));

Systemoutprintln(month);

//输出日

String day = datetimesubstring(datetimelastIndexOf("-")+1, datetimelength());

Systemoutprintln(day);

}

}

第二种方法:

Date today = new Date();

long time = todaygetTime();

Calendar calendar = CalendargetInstance();

calendarsetTimeInMillis(time);

//输出年

Systemoutprintln(calendarget(CalendarYEAR));

//输出月 :由于月份是从0开始的所以要加上1

Systemoutprintln(calendarget(CalendarMONTH+1));

//输出日

Systemoutprintln(calendarget(CalendarDAY_OF_MONTH));

希望能够帮助你解决问题

您好!

对于问题一:

这个方法的意思是系统当前时间与1970年1月1日0点0分0秒的毫秒差

对于问题二:

下面是实现的方法之一

public static void main(String[] args)

{

Calendar c = CalendargetInstance();

//现在的时间(单位:毫秒)

long nowMills = cgetTimeInMillis();

//第二个参数是设置月的,月是基于0的

//arg list:year,month,day,hour,minute,second

cset(2011, 4, 4, 23, 59, 59);

long setMills = cgetTimeInMillis();

long a=setMills-nowMills;

Systemoutprintln("2011年5月4日距当前时间分钟数:"+a/(601000));

}

希望对你有帮助!

这个是我以前自己做的,你看看吧,希望对你有帮助

//导入包

import javaxswing;

import javautil;

import javaapplet;

import javaawt;

import javaawteventWindowAdapter;

import javaawteventWindowEvent;

import javatext;

public class MyClock extends Applet implements Runnable

{

private volatile Thread timer;

//定义变量

private int lastxs,lastys,lastxm,lastym,lastxh,lastyh;

private SimpleDateFormat formatter;

private String lastdate;

private Font clockFaceFont;

private Date currentDate;

private Color handColor;

private Color numberColor;

private int xcenter=80,ycenter=55;

public void init(){

int x,y;

lastxs=lastys=lastxm=lastym=lastxh=lastyh=0;

formatter=new SimpleDateFormat("EEE MMM dd hh:ss yyyy",LocalegetDefault());//设置时间格式

currentDate=new Date();//获取时间

lastdate=formatterformat(currentDate);

clockFaceFont=new Font("Serif",FontPLAIN,14);

handColor=ColorBLUE;

numberColor=ColordarkGray;

try{

setBackground(new Color(IntegerparseInt(getParameter("bgcolor"),16)));

}catch(NullPointerException e){

}catch(NumberFormatException e){

}

try{

handColor=new Color(IntegerparseInt(getParameter("fgcolor1"),16));

}catch(NullPointerException e){

}catch(NumberFormatException e){

}

try{

numberColor=new Color(IntegerparseInt(getParameter("fgcolor2"),16));

}catch(NullPointerException e){

}catch(NumberFormatException e){

}

resize(300,300);

}

public void update(Graphics g){

int xh,yh,xm,ym,xs,ys;

int s=0,m=10,h=10;

String today;

currentDate=new Date();

formatterapplyPattern("s");

try{

s=IntegerparseInt(formatterformat(currentDate));

}catch(NumberFormatException n){

s=0;

}

formatterapplyPattern("m");

try{

m=IntegerparseInt(formatterformat(currentDate));

}catch(NumberFormatException n){

m=10;

}

formatterapplyPattern("h");

try{

h=IntegerparseInt(formatterformat(currentDate));

}catch(NumberFormatException n){

h=10;

}

xs=(int)(Mathcos(sMathPI/30-MathPI/2)45+xcenter);

ys=(int)(Mathsin(sMathPI/30-MathPI/2)45+ycenter);

xm=(int)(Mathcos(mMathPI/30-MathPI/2)40+xcenter);

ym=(int)(Mathsin(mMathPI/30-MathPI/2)40+ycenter);

xh=(int)(Mathcos((h30+m/2)MathPI/180-MathPI/2)30+xcenter);

yh=(int)(Mathsin((h30+m/2)MathPI/180-MathPI/2)30+ycenter);

//获取在底部显示的日期

formatterapplyPattern("EEE MMM dd HH: mm:ss yyyy");

today=formatterformat(currentDate);

gsetFont(clockFaceFont);

gsetColor(getBackground());

if(xs!=lastxs||ys!=lastys)

{

gdrawLine(xcenter,ycenter,lastxs,lastys);

gdrawString(lastdate,5,125);

}

if(xm!=lastxm||ym!=lastym)

{

gdrawLine(xcenter,ycenter-1,lastxm,lastym);

gdrawLine(xcenter-1,ycenter,lastxm,lastym);

}

if(xh!=lastxh||yh!=lastyh)

{

gdrawLine(xcenter,ycenter-1,lastxh,lastyh);

gdrawLine(xcenter-1,ycenter,lastxh,lastyh);

}

gsetColor(numberColor);

gdrawString(today,5,125);

gdrawLine(xcenter,ycenter,xs,ys);

gsetColor(handColor);

gdrawLine(xcenter,ycenter-1,xm,ym);

gdrawLine(xcenter-1,ycenter,xm,ym);

gdrawLine(xcenter,ycenter-1,xh,yh);

gdrawLine(xcenter-1,ycenter,xh,yh);

lastxs=xs;lastys=ys;

lastxm=xh;lastym=ym;

lastdate=today;

}

public void paint(Graphics g){

gsetFont(clockFaceFont);

gsetColor(handColor);

gdrawArc(xcenter-50,ycenter-50,100,100,0,360);

gsetColor(numberColor);

gdrawString("9",xcenter-45,ycenter+3);

gdrawString("3",xcenter+40,ycenter+3);

gdrawString("12",xcenter-5,ycenter-37);

gdrawString("6",xcenter-3,ycenter+45);

gsetColor(numberColor);

gdrawString(lastdate,5,125);

gdrawLine(xcenter,ycenter,lastxs,lastys);

gsetColor(handColor);

gdrawLine(xcenter,ycenter-1,lastxm,lastym);

gdrawLine(xcenter-1,ycenter,lastxm,lastym);

gdrawLine(xcenter,ycenter-1,lastxh,lastyh);

gdrawLine(xcenter-1,ycenter,lastxh,lastyh);

}

public void start(){

timer=new Thread(this);

timerstart();

}

public void stop(){

timer=null;

}

public void run() {

//To change body of implemented methods use File | Settings | File Templates

Thread me=ThreadcurrentThread();

while(timer==me){

try{

ThreadcurrentThread()sleep(100);

}catch(InterruptedException e){

}

repaint();

}

}

public static void main(String[] args) {

JFrame f=new JFrame("显示时钟");

fsetSize(200,180);

MyClock mc=new MyClock();

mcinit();

mcstart();

fadd(mc);

/ faddWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

Systemexit(0);

}

});/

fsetDefaultCloseOperation(3);

fshow();

}

}

java在当前系统时间加一天主要是使用calendar类的add方法,如下代码:import javautilCalendar;import javautilDate;public class ceshi { public static void main(String[] args) { Date date = new Date();// 新建此时的的系统时间 Systemoutprintln(getNextDay(date));// 返回明天的时间 } public static Date getNextDay(Date date) { Calendar calendar = CalendargetInstance(); calendarsetTime(date); calendaradd(CalendarDAY_OF_MONTH, +1);//+1今天的时间加一天 date = calendargetTime(); return date; }}运行结果:

Calendar 类就是 *** 作日期时间的类,你获取一个Calendar 的实例就可以了。常用的变量有DATE,HOUR,MINUTE,MONTH,YEAR,DATE_OF_MONTH,ZONE_OFFSET,方法有add,set,roll,get等

以上就是关于Java获取时间戳精确到年月日时全部的内容,包括:Java获取时间戳精确到年月日时、java 如何从Long型的时间中取得年月日、Java菜鸟问题(与获取计算机系统时间相关)等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9656120.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存