导航:首页 > 编程语言 > java获取当前日期和时间

java获取当前日期和时间

发布时间:2021-04-19 13:29:54

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中获取当前日期和时间的方法

import java.util.Date;
import java.util.Calendar;

import java.text.SimpleDateFormat;

public class TestDate{
public static void main(String[] args){
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//可以方便地修改日期格式

String hehe = dateFormat.format( now );
System.out.println(hehe);

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);
}
}

有时候要把String类型的时间转换为Date类型,通过以下的方式,就可以将你刚得到的时间字符串转换为Date类型了。

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

java.util.Date time=null;
try {
time= sdf.parse(sdf.format(new Date()));

} catch (ParseException e) {

e.printStackTrace();
}

㈡ java如何获取当前精确时间

java获取当前时间精确到毫秒newSimpleDateFormat("yyyyMMddHHmmssSSS").format(newDate());

方法2:
CalendarCld=Calendar.getInstance();

intYY=Cld.get(Calendar.YEAR);
intMM=Cld.get(Calendar.MONTH)+1;
intDD=Cld.get(Calendar.DATE);
intHH=Cld.get(Calendar.HOUR_OF_DAY);
intmm=Cld.get(Calendar.MINUTE);
intSS=Cld.get(Calendar.SECOND);
intMI=Cld.get(Calendar.MILLISECOND);

StringcurTime=YY+MM+DD+HH+mm+SS+MI;Calendarcal=Calendar.getInstance();
java.util.Datedate=cal.getTime();

SimpleDateFormatsdFormat=newSimpleDateFormat("yyyyMMddhhmmssSSS");

StringmyTime=sdFormat.format(currentTime);

㈢ java获取当前时间

因为你的这来一句Calendar c = Calendar.getInstance();是写在源循环外面的,声明了c以后那c就是这一刻的时间,无论你再怎么循环还是只打印声明c时的时间。这段代码我没太看明白你要做什么,不过你想循环输出当前的时间可以这样写:

while(true){
Calendarc=Calendar.getInstance();
inttime=c.get(Calendar.SECOND);
System.out.println(time);
}

把Calendar c = Calendar.getInstance();写在循环里就是不停的循环获得当前时间。

㈣ java打印当前日期和时间

package time;
import java.util.Date;
import java.util.Calendar;

import java.text.SimpleDateFormat;

public class TestDate{
public static void main(String[] args){
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//可以方便地修改日期格式
SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy/MM/dd");

SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy");

SimpleDateFormat dateFormat3 = new SimpleDateFormat("MM/dd");

String time = dateFormat.format( now );
String date = dateFormat1.format( now );
System.out.println(time);
System.out.println(date);
/*
Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int date1 = 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 + "-" + date1 + " " +hour + ":" +minute + ":" + second);
*/
}
}

㈤ java如何获取当前时间 年月日 时分秒

//得到类型当前时间

longl=System.currentTimeMillis();

//new日期对

Datedate=newDate(l);

//转换提日期输出格式

SimpleDateFormatdateFormat=newSimpleDateFormat("yyyy-MM-

ddHH:mm:ss");System.out.println(dateFormat.format(date));

(5)java获取当前日期和时间扩展阅读

package com.ob;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

public class DateTest {

public static void main(String[] args) throws ParseException {

Calendar now = Calendar.getInstance();

System.out.println("年: " + now.get(Calendar.YEAR));

System.out.println("月: " + (now.get(Calendar.MONTH) + 1) + "");

System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH));

System.out.println("时: " + now.get(Calendar.HOUR_OF_DAY));

System.out.println("分: " + now.get(Calendar.MINUTE));

System.out.println("秒: " + now.get(Calendar.SECOND));

System.out.println("当前时间毫秒数:" + now.getTimeInMillis());

System.out.println(now.getTime());

Date d = new Date();

System.out.println(d);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateNowStr = sdf.format(d);

System.out.println("格式化后的日期:" + dateNowStr);

String str = "2012-1-13 17:26:33";

//要跟上面sdf定义的格式一样
Date today = sdf.parse(str);

System.out.println("字符串转成日期:" + today);
}
}

㈥ 如何获取当前的日期和时间

什么语言?
js的话如下:
Js获取当前日期时间及其它操作

var myDate = new
Date();
myDate.getYear();
//获取当前年份(2位)
myDate.getFullYear();
//获取完整的年份(4位,1970-????)
myDate.getMonth();
//获取当前月份(0-11,0代表1月)
myDate.getDate();
//获取当前日(1-31)
myDate.getDay();
//获取当前星期X(0-6,0代表星期天)
myDate.getTime();
//获取当前时间(从1970.1.1开始的毫秒数)
myDate.getHours();
//获取当前小时数(0-23)
myDate.getMinutes();
//获取当前分钟数(0-59)
myDate.getSeconds();
//获取当前秒数(0-59)
myDate.getMilliseconds();
//获取当前毫秒数(0-999)
myDate.toLocaleDateString();
//获取当前日期
var mytime=myDate.toLocaleTimeString();
//获取当前时间
myDate.toLocaleString( );
//获取日期与时间

㈦ 请问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()为获取当前系统时间
}
}

㈧ java 获取当前日期,应该如何操作呢

package util;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
* 获取系统时间
*

*/
public class DateUtil {
/* 日志对象 */
// private static Logger logger = Logger.getLogger(SystemUtil.class);
/* 获取年份 */
public static final int YEAR = 1;
/* 获取年月 */
public static final int YEARMONTH = 2;
/* 获取年月日 */
public static final int YEARMONTHDAY = 3;
/* 获取年月日,小时 */
public static final int YMD_HOUR = 4;
/* 获取年月日,小时,分钟 */
public static final int YMD_HOURMINUTE = 5;
/* 获取年月日,时分秒 */
public static final int FULL = 6;
/* 获取年月日时分秒 格式:yyyyMMddHHmmss */
public static final int UTILTIME = 7;

/**
* 根据指定时间格式类型得到当前时间
*
* @param type
* 时间类型
* @return String 字符串时间
*/
public static synchronized String getCurrentTime(int type) {
String format = getFormat(type);
SimpleDateFormat timeformat = new SimpleDateFormat(format);
Date date = new Date();
return timeformat.format(date);
}

/**
* 返回当前系统时间的年月日
*
* @return
*/
public static synchronized String getCurrentTime() {
SimpleDateFormat timeformat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
return timeformat.format(date);
}
/**
* 根据参数格式,格式化当前日期
* @param format
* @return
*/
public static synchronized String getDateString(String format) {
SimpleDateFormat timeformat = new SimpleDateFormat(format);
Date date = new Date();
return timeformat.format(date);
}

/**
* 根据指定时间格式类型,格式化时间格式
*
* @param type
* 时间格式类型
* @return
*/
private static String getFormat(int type) {
String format = "";
if (type == 1) {
format = "yyyy";
} else if (type == 2) {
format = "yyyy-MM";
} else if (type == 3) {
format = "yyyy-MM-dd";
} else if (type == 4) {
format = "yyyy-MM-dd HH";
} else if (type == 5) {
format = "yyyy-MM-dd HH:mm";
} else if (type == 6) {
format = "yyyy-MM-dd HH:mm:ss";
} else if (type == 7) {
format = "yyyyMMddHHmmss";
} else {
throw new RuntimeException("日期格式参数错误");
}
return format;
}

public static int getYear(String dateString) {
SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = dd.parse(dateString);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.YEAR);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static int getMonth(String dateString) {
SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = dd.parse(dateString);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.MONTH)+1;
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public static int getDay(String dateString) {
SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = dd.parse(dateString);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.DAY_OF_MONTH);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public static Date StringToDate(String dateStr, String formatStr) {
SimpleDateFormat dd = new SimpleDateFormat(formatStr);
Date date = null;
try {
date = dd.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}

/**
* 当前日期和参数日期距离的小时数 日期格式:yyyy-MM-dd HH:mm:ss
*
* @param date
* @return
*/
public static double getHours(String date) {
SimpleDateFormat timeformat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
try {
Date d = new Date();
Date d1 = timeformat.parse(date);

long temp = d.getTime() - d1.getTime();
double f = temp / 3600000d;
BigDecimal b = new BigDecimal(f);
double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
return f1;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}

public static void main(String a[]) {
try {
int aa = getYear("2012-01-08");
System.out.println(aa);
} catch (Exception e) {
e.printStackTrace();
}
}
}

㈨ Java代码中如何获得当前时间

java.util.Date nowdate = new java.util.Date();然后如果你想时间的格式和你想用的时间格式一致 那么就要格式化时间了SimpleDateFormat 的包内在java.text包下SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") //年月日 时分容秒String t = sdf.parse(nowdate);

㈩ Java 获取当前时间的小时(24小时制)

使用new Date()获取时间,通过SimpleDateFormat格式化类对Date进行格式话时间。

具体代码内如下:注意HH大写代表24小时制容。

输出结果:1510416000000,2017-11-12。方便的实现了string转时间的功能。

阅读全文

与java获取当前日期和时间相关的资料

热点内容
vb编写一个简单计算器程序代码 浏览:381
app代充怎么赚钱 浏览:133
湖南省大数据发展 浏览:838
ip和数据哪个好看 浏览:409
linux文件驱动 浏览:511
超大数据中心 浏览:697
工作文件系统如何建立 浏览:307
利用文件中的内容初始化 浏览:935
马云支付宝用到的大数据技术 浏览:333
厦门大数据战略 浏览:720
6s如何设置app切换 浏览:724
西门子编程软件在官方网站哪里找 浏览:511
大数据社会调研报告 浏览:172
数据中的属性有哪些类型 浏览:176
苹果6手机支付宝加密 浏览:480
大数据的内涵以下理解 浏览:92
word2007组合 浏览:643
定向士官在什么网站报志愿填报 浏览:332
hyp是什么文件格式 浏览:720
编程哪里学靠谱 浏览:224

友情链接