「java格式化当前时间」java将日期格式化

博主:adminadmin 2022-11-22 18:56:06 726

本篇文章给大家谈谈java格式化当前时间,以及java将日期格式化对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java日期格式化问题?

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");\x0d\x0aDate currentTime = new Date();// 得到当前系统时间\x0d\x0aString str_date = formatter.format(currentTime); // 将日期时间格式化\x0d\x0aSystem.out.print(str_date );\x0d\x0a这样肯定没问题啊

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

一. 获取当前系统时间和日期并格式化输出:\x0d\x0a\x0d\x0aimport java.util.Date; \x0d\x0aimport java.text.SimpleDateFormat;\x0d\x0a\x0d\x0apublic class NowString { \x0d\x0a public static void main(String[] args) { \x0d\x0a SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式\x0d\x0a System.out.println(df.format(new Date()));// new Date()为获取当前系统时间\x0d\x0a } \x0d\x0a} \x0d\x0a\x0d\x0a二. 在数据库里的日期只以年-月-日的方式输出,可以用下面两种方法:\x0d\x0a\x0d\x0a1、用convert()转化函数:\x0d\x0a\x0d\x0aString sqlst = "select convert(varchar(10),bookDate,126) as convertBookDate from roomBook where bookDate between '2007-4-10' and '2007-4-25'";\x0d\x0a\x0d\x0aSystem.out.println(rs.getString("convertBookDate")); \x0d\x0a\x0d\x0a2、利用SimpleDateFormat类:\x0d\x0a\x0d\x0a先要输入两个java包:\x0d\x0a\x0d\x0aimport java.util.Date; \x0d\x0aimport java.text.SimpleDateFormat;\x0d\x0a\x0d\x0a然后:\x0d\x0a\x0d\x0a定义日期格式:SimpleDateFormat sdf = new SimpleDateFormat(yy-MM-dd);\x0d\x0a\x0d\x0asql语句为:String sqlStr = "select bookDate from roomBook where bookDate between '2007-4-10' and '2007-4-25'";\x0d\x0a\x0d\x0a输出:\x0d\x0a\x0d\x0aSystem.out.println(df.format(rs.getDate("bookDate")));

JAVA 中获取时间怎么格式化?

时间格式化输出主要有两种方式,代码如下:

//使用Calendar

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

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

//使用Date

Date d = new Date();

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

System.out.println("当前时间:" + sdf.format(d));

扩展资料

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

java格式化当前时间的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java将日期格式化、java格式化当前时间的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-22,除非注明,否则均为首码项目网原创文章,转载请注明出处。