JAVA中获取当前系统时间
来源:互联网 发布:ei数据库官网 编辑:程序博客网 时间:2024/05/17 01:27
一.获取当前系统时间和日期并格式化输出:
importjava.util.Date;
importjava.text.SimpleDateFormat;
public classNowString {
public staticvoid main(String[] args) {
SimpleDateFormatdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
System.out.println(df.format(newDate()));// new Date()为获取当前系统时间
}
}
二.在数据库里的日期只以年-月-日的方式输出,可以用下面两种方法:
1、用convert()转化函数:
String sqlst ="select convert(varchar(10),bookDate,126) as convertBookDate from roomBookwhere bookDate between '2007-4-10' and '2007-4-25'";
System.out.println(rs.getString("convertBookDate"));
2、利用SimpleDateFormat类:
先要输入两个java包:
importjava.util.Date;
importjava.text.SimpleDateFormat;
然后:
定义日期格式:SimpleDateFormatsdf = new SimpleDateFormat(yy-MM-dd);
sql语句为:StringsqlStr = "select bookDate from roomBook where bookDate between '2007-4-10'and '2007-4-25'";
输出:
System.out.println(df.format(rs.getDate("bookDate")));
************************************************************
java中获取当前日期和时间的方法
importjava.util.Date;
importjava.util.Calendar;
importjava.text.SimpleDateFormat;
publicclass TestDate{
publicstatic void main(String[] args){
Datenow = new Date();
SimpleDateFormatdateFormat = new SimpleDateFormat("yyyy/MM/ddHH:mm:ss");//可以方便地修改日期格式
Stringhehe = dateFormat.format( now );
System.out.println(hehe);
Calendarc = Calendar.getInstance();//可以对每个时间域单独修改
intyear = c.get(Calendar.YEAR);
intmonth = c.get(Calendar.MONTH);
intdate = c.get(Calendar.DATE);
inthour = c.get(Calendar.HOUR_OF_DAY);
intminute = c.get(Calendar.MINUTE);
intsecond = c.get(Calendar.SECOND);
System.out.println(year+ "/" + month + "/" + date + " " +hour +":" +minute + ":" + second);
}
}
有时候要把String类型的时间转换为Date类型,通过以下的方式,就可以将你刚得到的时间字符串转换为Date类型了。
SimpleDateFormatsdf=new SimpleDateFormat("yyyy-MM-dd");
java.util.Datetime=null;
try{
time= sdf.parse(sdf.format(new Date()));
}catch (ParseException e) {
e.printStackTrace();
}
- Java中获取系统当前时间
- JAVA中获取当前系统时间
- JAVA中获取当前系统时间
- JAVA中获取当前系统时间
- JAVA中获取当前系统时间
- Java中获取当前系统时间
- JAVA中获取当前系统时间
- JAVA中获取当前系统时间
- JAVA中获取当前系统时间
- JAVA中获取当前系统时间
- JAVA中获取当前系统时间
- JAVA中获取当前系统时间
- java中获取系统当前时间
- JAVA中获取当前系统时间
- JAVA中获取当前系统时间
- JAVA中获取当前系统时间
- JAVA中获取当前系统时间
- JAVA中获取当前系统时间
- 解决透视变换后图片信息丢失的问题
- 基于贝叶斯判决的肤色检测
- 操作系统中的锁
- 添加额外的源, 使得yum可以安装更多的软件
- I.MX6Q(TQIMX6Q/TQE9)学习笔记——新版BSP之UART移植
- JAVA中获取当前系统时间
- 【剑指offer】复杂链表的复制
- MYSQL 锁
- libevent总结(下)
- mac os + sublime text3 搭建 golang 环境
- HTML5 Server-Sent Events with JSP example
- 二进制学习差分进化算法(BLDE)的R语言实现
- PRD实用案例|「赶公交」App产品需求文档
- adb logcat命令行用法