网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

用java写时间代码 java实现时间时钟

编写一个java程序,读取系统时间,然后将时间用中文输出

java程序读取系统时间,可以使用Date时间类,使用格式化类simpleDateFormat类,实例如下:

站在用户的角度思考问题,与客户深入沟通,找到梁平网站设计与梁平网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都做网站、成都网站设计、成都外贸网站建设、企业官网、英文网站、手机端网站、网站推广、域名注册虚拟主机、企业邮箱。业务覆盖梁平地区。

package com.qiu.lin.he;

import java.text.SimpleDateFormat;

import java.util.Date;

public class CeShi {

public static void main(String[] args) {

Date date = new Date();//获取此时的系统时间

SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH点mm分ss秒");

System.out.println("中文现实的时间是" + sdf.format(date));

}

}

运行结果如下:

JAVA中获取系统当前时间该怎么写?

一. 获取当前系统时间和日期并格式化输出:

import java.util.Date;

import java.text.SimpleDateFormat;

public class NowString {

public static void main(String[] args) {

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式

System.out.println(df.format(new Date()));// new Date()为获取当前系统时间

}

}

二. 在数据库里的日期只以年-月-日的方式输出,可以用下面两种方法:

1、用convert()转化函数:

String sqlst = "select convert(varchar(10),bookDate,126) as convertBookDate from roomBook where bookDate between '2007-4-10' and '2007-4-25'";

System.out.println(rs.getString("convertBookDate"));

2、利用SimpleDateFormat类:

先要输入两个java包:

import java.util.Date;

import java.text.SimpleDateFormat;

然后:

定义日期格式:SimpleDateFormat sdf = new SimpleDateFormat(yy-MM-dd);

sql语句为:String sqlStr = "select bookDate from roomBook where bookDate between '2007-4-10' and '2007-4-25'";

输出:

System.out.println(df.format(rs.getDate("bookDate")));

请问JAVA中获取系统当前时间该怎么写

有两种方法:

方法一:用java.util.Date类来实现,并结合java.text.DateFormat类来实现时间的格式化,看下面代码:

import java.util.*;

import java.text.*;

//以下默认时间日期显示方式都是汉语语言方式

//一般语言就默认汉语就可以了,时间日期的格式默认为MEDIUM风格,比如:2008-6-16 20:54:53

//以下显示的日期时间都是再Date类的基础上的来的,还可以利用Calendar类来实现见类TestDate2.java

public class TestDate {

public static void main(String[] args) {

Date now = new Date();

Calendar cal = Calendar.getInstance();

DateFormat d1 = DateFormat.getDateInstance(); //默认语言(汉语)下的默认风格(MEDIUM风格,比如:2008-6-16 20:54:53)

String str1 = d1.format(now);

DateFormat d2 = DateFormat.getDateTimeInstance();

String str2 = d2.format(now);

DateFormat d3 = DateFormat.getTimeInstance();

String str3 = d3.format(now);

DateFormat d4 = DateFormat.getInstance(); //使用SHORT风格显示日期和时间

String str4 = d4.format(now);

DateFormat d5 = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL); //显示日期,周,时间(精确到秒)

String str5 = d5.format(now);

DateFormat d6 = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG); //显示日期。时间(精确到秒)

String str6 = d6.format(now);

DateFormat d7 = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT); //显示日期,时间(精确到分)

String str7 = d7.format(now);

DateFormat d8 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM); //显示日期,时间(精确到分)

String str8 = d8.format(now);//与SHORT风格相比,这种方式最好用

System.out.println("用Date方式显示时间: " + now);//此方法显示的结果和Calendar.getInstance().getTime()一样

System.out.println("用DateFormat.getDateInstance()格式化时间后为:" + str1);

System.out.println("用DateFormat.getDateTimeInstance()格式化时间后为:" + str2);

System.out.println("用DateFormat.getTimeInstance()格式化时间后为:" + str3);

System.out.println("用DateFormat.getInstance()格式化时间后为:" + str4);

System.out.println("用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化时间后为:" + str5);

System.out.println("用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化时间后为:" + str6);

System.out.println("用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化时间后为:" + str7);

System.out.println("用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化时间后为:" + str8);

}

}

运行结果:

用Date方式显示时间: Mon Jun 16 20:54:53 CST 2008

用DateFormat.getDateInstance()格式化时间后为:2008-6-16

用DateFormat.getDateTimeInstance()格式化时间后为:2008-6-16 20:54:53

用DateFormat.getTimeInstance()格式化时间后为:20:54:53

用DateFormat.getInstance()格式化时间后为:08-6-16 下午8:54

用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化时间后为

:2008年6月16日 星期一 下午08时54分53秒 CST

用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化时间后为

:2008年6月16日 下午08时54分53秒

用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化时间后

为:08-6-16 下午8:54

用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化时间

后为:2008-6-16 20:54:53

方法二:用java.util.Calendar类来实现,看下面:

import java.util.*;

import java.text.*;

//以下是利用Calendar类来实现日期时间的,和Date类相比较比较简单

public class TestDate2 {

public static void main(String[] args) {

Calendar ca = Calendar.getInstance();

int year = ca.get(Calendar.YEAR);//获取年份

int month=ca.get(Calendar.MONTH);//获取月份

int day=ca.get(Calendar.DATE);//获取日

int minute=ca.get(Calendar.MINUTE);//分

int hour=ca.get(Calendar.HOUR);//小时

int second=ca.get(Calendar.SECOND);//秒

int WeekOfYear = ca.get(Calendar.DAY_OF_WEEK);

System.out.println("用Calendar.getInstance().getTime()方式显示时间: " + ca.getTime());

System.out.println("用Calendar获得日期是:" + year +"年"+ month +"月"+ day + "日");

System.out.println("用Calendar获得时间是:" + hour +"时"+ minute +"分"+ second +"秒");

System.out.println(WeekOfYear);//显示今天是一周的第几天(我做的这个例子正好是周二,故结果显示2,如果你再周6运行,那么显示6)

}

}

运行结果是:

用Calendar.getInstance().getTime()方式显示时间: Mon Jun 16 21:54:21 CST 2008

用Calendar获得日期是:2008年5月16日

用Calendar获得时间是:9时54分21秒

2

总结:中的来说,方法二是最方便的,方法一显得分笨拙,不过看个人喜欢了。

java编写一个简单的输入生日计算下一个生日时间的代码?

import java.util.Calendar;

import java.util.Scanner;

import java.util.concurrent.TimeUnit;

/**

* Title: Test03.javabr

* Description:

*

* @author 王凯芳

* @date 2020年3月5日 下午6:03:04

* @version 1.0

*

* @request 编写一个方法能计算任何一个人今天离他最近下一次生日还有多少天,然后在主方法(main方法)中输入你的出生年月日,调用该方法的计算结果并输出信息“某某同学离自己最近下一次生日x天”。

*/

public class Test03 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("请输入你的姓名:");

String name = sc.nextLine();

System.out.println("请输入你的生日,格式为(2000/01/01):");

String line = sc.nextLine();

String[] strs = line.split("/");

if (strs.length == 3) {

int days = getDays(strs[0], strs[1], strs[2]);

if (days == 0) {

System.out.println(String.format("%s 同学,今天是你的生日,祝你生日快乐(#^.^#)", name, days));

} else {

System.out.println(String.format("%s 同学离自己最近下一次生日%d天。", name, days));

}

} else {

System.out.println("生日输入不正确!请按格式输入。");

}

sc.close();

}

/**

* 获取最近一次生日天数

*

* @param year

* @param month

* @param day

* @return

*/

public static int getDays(String year, String month, String day) {

Calendar now = Calendar.getInstance();

now.set(Calendar.HOUR_OF_DAY, 0);

now.set(Calendar.MINUTE, 0);

now.set(Calendar.SECOND, 0);

now.set(Calendar.MILLISECOND, 0);

int now_year = now.get(Calendar.YEAR);

Calendar birthday = Calendar.getInstance();

birthday.set(Calendar.YEAR, now_year);

birthday.set(Calendar.MONTH, Integer.parseInt(month) - 1);

birthday.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day));

birthday.set(Calendar.HOUR_OF_DAY, 0);

birthday.set(Calendar.MINUTE, 0);

birthday.set(Calendar.SECOND, 0);

birthday.set(Calendar.MILLISECOND, 0);

long diff = now.getTimeInMillis() - birthday.getTimeInMillis();

if (diff == 0) {

return 0;

} else if (diff 0) {

long diffDays = TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);

return Math.abs((int) diffDays);

} else {

birthday.add(Calendar.YEAR, 1);

long diffMi = birthday.getTimeInMillis() - now.getTimeInMillis();

long diffDays = TimeUnit.DAYS.convert(diffMi, TimeUnit.MILLISECONDS);

return (int) diffDays;

}

}

}


网页题目:用java写时间代码 java实现时间时钟
浏览地址:http://bjjierui.cn/article/ddcosge.html

其他资讯