JAVA对时间的操作

1.Date获取当前时间

  1.1将时间毫秒转为日期格式。

 import java.sql.Date;
Date d = new Date(System.currentTimeMillis());//传当前的毫秒时间
 String time=d.toLocaleString();//返回2015-8-17 11:08:26格式字符串

//使用SimpleDateFormat转换需要的格式
import java.text.SimpleDateFormat;
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println(sdf.format(longtime)); //输出2015-8-17 11:08:26

1.2获取时间示例

public void getTimeByDate(){
Date date = new Date();
DateFormat df1 = DateFormat.getDateInstance();//日期格式,精确到日
System.out.println(df1.format(date));
DateFormat df2 = DateFormat.getDateTimeInstance();//可以精确到时分秒
System.out.println(df2.format(date));
DateFormat df3 = DateFormat.getTimeInstance();//只显示出时分秒
System.out.println(df3.format(date));
DateFormat df4 = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL); //显示日期,周,上下午,时间(精确到秒)
System.out.println(df4.format(date));
DateFormat df5 = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG); //显示日期,上下午,时间(精确到秒) 

System.out.println(df5.format(date));
DateFormat df6 = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT); //显示日期,上下午,时间(精确到分)
System.out.println(df6.format(date));
DateFormat df7 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM); //显示日期,时间(精确到分)
System.out.println(df7.format(date));

  public void getTimeByCalendar(){
Calendar cal = Calendar.getInstance()
int year = cal.get(Calendar.YEAR);//获取年份
int month=cal.get(Calendar.MONTH);//获取月份
int day=cal.get(Calendar.DATE);//获取日
int hour=cal.get(Calendar.HOUR);//小时
int minute=cal.get(Calendar.MINUTE);//分
int second=cal.get(Calendar.SECOND);//秒
int WeekOfYear = cal.get(Calendar.DAY_OF_WEEK);//一周的第几天
System.out.println("现在的时间是:公元"+year+"年"+month+"月"+day+"日      "+hour+"时"+minute+"分"+second+"秒星期"+WeekOfYear);
            }

待续。。。

时间: 2024-08-08 09:34:26

JAVA对时间的操作的相关文章

关于时间的操作(Java版)——获取给定时间与当前系统时间的差值(以毫秒为单位)

import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; public class Test { /** * 获取给定时间与当前系统时间的差值(以毫秒为单位) * * @author GaoHuanjie */ public long getTimeDifferenceBetweenSystemTimeAndParamTime(String paramTime) { DateFor

关于时间的操作(Java版)——获取距离系统时间N天后的日期时间信息

import java.util.Calendar; import java.util.TimeZone; public class Test { /** * 获取距离系统时间N天后的日期时间信息 * * @author GaoHuanjie */ public String getDateAfterNdays(int days){ Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8")); cale

关于时间的操作(Java版)——将毫秒转换为年月日时分秒

第一种方式: import java.util.Calendar; import java.util.TimeZone; public class Test { /** * 将毫秒转换为年月日时分秒 * * @author GaoHuanjie */ public String getYearMonthDayHourMinuteSecond(long timeMillis) { Calendar calendar = Calendar.getInstance(TimeZone.getTimeZo

关于时间的操作(Java版)

本博文收录在编程过程中使用过的关于时间操作的Java代码: 1.获取给定日期N天后的日期 import java.util.Calendar; public class Test { public static void main(String[] args) { System.out.println(new Test().getDateAfterNDays("2012-05-10", 7));//输出2012-5-17 } /** * 获取给定日期N天后的日期 * * @author

Java日期时间操作源码示例大全

在研发闲暇时间,将开发过程比较重要的一些代码段做个记录,如下代码内容是关于Java日期时间操作示例大全的代码,应该是对小伙伴们有所用途. 日期类 import java.util.Calendar; public class VeDate { public static Date getNowDate() { Date currentTime = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd H

java 对mongodb的操作

java 对mongodb的操作 1.1连单台mongodb Mongo mg = newMongo();//默认连本机127.0.0.1  端口为27017 Mongo mg = newMongo(ip);//可以指定ip 端口默认为27017 Mongo mg = newMongo(ip,port);//也可以指定ip及端口号 1.2连双台mongodb //ip为主机ip地址,port为端口号,dataBaseName相当于数据库名 DBAddress left = new DBAddre

mysql和java的时间对应关系

引用:http://blog.csdn.net/xinghuo0007/article/details/51500923 MySQL(版本:5.1.50)的时间日期类型如下: datetime 8bytes xxxx-xx-xx xx:xx:xx 1000-01-01 00:00:00到9999-12-31 23:59:59 timestamp 4bytes xxxx-xx-xx xx:xx:xx 1970-01-01 00:00:01到2038 date 3bytes xxxx-xx-xx 1

java大文件读写操作,java nio 之MappedByteBuffer,高效文件/内存映射

java处理大文件,一般用BufferedReader,BufferedInputStream这类带缓冲的Io类,不过如果文件超大的话,更快的方式是采用MappedByteBuffer. MappedByteBuffer是java nio引入的文件内存映射方案,读写性能极高.NIO最主要的就是实现了对异步操作的支持.其中一种通过把一个套接字通道(SocketChannel)注册到一个选择器(Selector)中,不时调用后者的选择(select)方法就能返回满足的选择键(SelectionKey

java file文件类操作使用方法大全

1.构造函数 [java] view plaincopy public class FileDemo { public static void main(String[] args){ //构造函数File(String pathname) File f1 =new File("c:\\zuidaima\\1.txt"); //File(String parent,String child) File f2 =new File("c:\\zuidaima",&quo