当前位置:首页 » 编程语言 » javadate

javadate

发布时间: 2022-01-10 11:50:08

java中Date的使用

java语言中的date类介绍及使用
在JDK1.0中,Date类是唯一的一个代表时间的类,但是由于Date类不便于实现国际化,所以从JDK1.1版本开始,推荐使用Calendar类进行时间和日期处理。这里简单介绍一下Date类的使用。
1、使用Date类代表当前系统时间
Date d = new Date();
System.out.println(d);
使用Date类的默认构造方法创建出的对象就代表当前时间,由于Date类覆盖了toString方法,所以可以直接输出Date类型的对象,显示的结果如下:
Sun Mar 08 16:35:58 CST 2009
在该格式中,Sun代表Sunday(周日),Mar代表March(三月),08代表8号,CST代表China Standard Time(中国标准时间,也就是北京时间(东八区))。
2、使用Date类代表指定的时间
Date d1 = new Date(2009-1900,3-1,9);
System.out.println(d1);
使用带参数的构造方法,可以构造指定日期的Date类对象,Date类中年份的参数应该是实际需要代表的年份减去1900,实际需要代表的月份减去1以后的值。例如上面的示例代码代表就是2009年3月9号。
实际代表具体的年月日时分秒的日期对象,和这个类似。
3、获得Date对象中的信息
Date d2 = new Date();
//年份
int year = d2.getYear() + 1900;
//月份
int month = d2.getMonth() + 1;
//日期
int date = d2.getDate();
//小时
int hour = d2.getHours();
//分钟
int minute = d2.getMinutes();
//秒
int second = d2.getSeconds();
//星期几
int day = d2.getDay();
System.out.println("年份:" + year);
System.out.println("月份:" + month);
System.out.println("日期:" + date);
System.out.println("小时:" + hour);
System.out.println("分钟:" + minute);
System.out.println("秒:" + second);
System.out.println("星期:" + day);
使用Date类中对应的get方法,可以获得Date类对象中相关的信息,需要注意的是使用getYear获得是Date对象中年份减去1900以后的值,所以需要显示对应的年份则需要在返回值的基础上加上1900,月份类似。在Date类中还提供了getDay方法,用于获得Date对象代表的时间是星期几,Date类规定周日是0,周一是1,周二是2,后续的依次类推。
4、Date对象和相对时间之间的互转
Date d3 = new Date(2009-1900,3-1,10);
long time = 1290876532190L;
//将Date类的对象转换为相对时间
long t = d3.getTime();
System.out.println(t);
//将相对时间转换为Date类的对象
Date d4 = new Date(time);
System.out.println(d4);
使用Date对象中的getTime方法,可以将Date类的对象转换为相对时间,使用Date类的构造方法,可以将相对时间转换为Date类的对象。经过转换以后,既方便了时间的计算,也使时间显示比较直观了。

Ⅱ java语言中的date类及方法的用法

Date和Calendar是Java类库里提供对时间进行处理的类,由于日期在商业逻辑的应用中占据着很重要的地位,所以在这里想对这两个类进行一个基本的讲解,由于技术有限,不到之处请指正。

Date类顾名思义,一看就知道是和日期有关的类了,这个类最主要的作用就是获得当前时间了,然而这个类里面也具有设置时间以及一些其他的功能,可是由于本身设计的问题,这些方法却遭到众多批评,而这些遭受批评的功能都已移植到另外一个类里面,这就是今天要讲到的第二个类Calendar里面。

在讲两个类之前,这里又不能不多提一个类,那就是DateFormat类,这个类是用来格式化日期的,稍后也会讲到。

首先,让我们来看一个获取当前时间的例子:

Date date = new Date();
System.out.println(date.getTime());上面的语句首先创建了Date的一个对象,接着使用getTime方法获得当前的时间,但是注意了,输出后的结果确实一串长整型的数字,这是为什么?实际上这是系统根据当前时间计算出来的一个long型的数,至于是如何计算出来的就不在本文中讲述了,那既然这样的话又如何显示正确的时间呢?这就要利用到上面的DateFormat类了,这个类是一个基类,它有一个子类是SimpleDateFormat,具体用法请看下面的代码:

Date date = new Date();
SimpleDateFormat dateFm = new SimpleDateFormat("EEEE-MMMM-dd-yyyy");
System.out.println(dateFm.format(date));这段代码开始创建了一个Date的对象,用来获取当前时间,而重点就在于后面的SimpleDateFormat对象,这个对继承了DateFormat,利用format方法对Date对象进行格式化,然后输出,而格式的定制是由用户定制的,EEEE代表星期,MMMM代表月份,而dd代表日,yyyy代表年。使用这个方法就可以根据用户自定义的格式进行输出时间。

上面介绍了由用户自定义格式的输出时间,下面将来介绍通过JAVA类库提供的标准格式输出时间,这就要用到DateFormat类了,请看以下代码:

Date date = new Date();
DateFormat dateFm = DateFormat.getDateTimeInstance(DateFormat.SHORT,
DateFormat.SHORT);
System.out.println(dateFm.format(date));这里使用的方法和用户自定义的方法差不多,只是这里使用的是一个抽象类,由于DateFormat是一个抽象类,所以它不能通过构造函数构造对象,在这里是通过getDateTimeInstance()方法获得该对象,而所传递的参数就是DateFormat里面定义的一些常量,系统根据这些常量输出当前时间,由于这里使用的是getDateTimeInstance方法,所以将传递两个常量参数,用来分别格式化日期和当前的时间。

上面讲述了如何获得系统时间以及如何格式化输出,那如果想获取或者设置时间当中的某一部分又该如何呢?例如年,月,日。这就要靠Calendar这个类了,这个类也是一个抽象类,它有一个子类GregorianCalendar,接下来我会利用这个子类来演示这个过程,请看以下代码:

DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL);

GregorianCalendar cal = new GregorianCalendar();

cal.setTime(new Date());

System.out.println("System Date: " + dateFormat.format(cal.getTime()));

cal.set(GregorianCalendar.DAY_OF_WEEK,GregorianCalendar.FRIDAY);
System.out.println("After Setting Day of Week to Friday: " +
dateFormat.format(cal.getTime()));
这段代码当中,首先创建了一个DateFormat对象进行格式设置,接着创建了一个GregorianCalendar对象cal,接着使用cal.setTime()方法设置cal对象中的时间为当前时间,然后通过format格式化由cal.getTime()返回的时间进行输出,后面利用set方法设置cal的日期为当前星期的FRIDAY,此时cal中存储的时间就是这个星期五的该时刻,而后面利用format格式化输出,假如当前时间为2005年1月27日星期4的11点30分,那么最后将那句将会输出2005年1月28日星期5的11点30分。

Ⅲ java date 是什么时间

java 得到系统时间,直接私用Date类型,直接生成一个对象即可,示例如下:

1
2
3
4
5
6
7

import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
Date dt=new Date();//如果不需要格式,可直接用dt,dt就是当前系统时间
DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//设置显示格式
String nowTime="";
nowTime= df.format(dt);//用DateFormat的format()方法在dt中获取并以yyyy/MM/dd HH:mm:ss格式显示

Ⅳ 在java的程序里date类型怎么比较大小呀

有两个方法:

方法一:

两个Date类型的变量可以通过compareTo方法来比较。此方法的描述是这样的:如果参数 Date 等于此 Date,则返回值 0;如果此 Date 在 Date 参数之前,则返回小于 0 的值;如果此 Date 在 Date 参数之后,则返回大于 0 的值。

(4)javadate扩展阅读:

Date类可以在java.util包中找到,用一个long类型的值表示一个指定的时刻。它的一个有用的构造函数是Date(),它创建一个表示创建时刻的对象。

getTime()方法返回Date对象的long值。在下面的程序中,我使用Date()构造函数创建一个表示程序运行时刻的对象,并且利用getTime()方法找到这个日期代表的毫秒数量:

import java.util.*;

public class Now {

public static void main(String[] args) {

Date now = new Date();

long nowLong = now.getTime();

System.out.println("Value is " + nowLong);

}

}

Ⅳ java,Date要怎么输入

首先get方法是获取值,set方法是设置值,你这里是要设置值所以应该用set方法

其次你要set一个Date类型的值,Date类型的值有多种创建方式:

Datedate1=newDate();//当前时间
Datedate2=newDate(10000);//1970年1月1日之后10000毫秒时的时间
SimpleDateFormatdateFormat=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");
Datedate3=dateFormat.parse("2018-04-0621:34:55");
Calendarcalendar=Calendar.getInstance();
calendar.set(2018,3,6,21,34,55);//这里的月份从0开始,所以填3实际表示的4月份
Datedate4=calendar.getTime();

Ⅵ java 中的date怎么用

给你写一个:
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String s1 = sdf.format(date);//这里得到:26/03/1999 这个格式的日期
sdf = new SimpleDateFormat("HH:mm");
String s2=sdf.format(date);//这里得到的是 18:00 这个格式的时间

List<String> list = new ArrayList<String>();//然后创建一个ArrayList();
list.add(s1);
list.add(s2);//把他们添加到list
谢谢

Ⅶ java中的Date类为什么很多方法被废弃了

Date类中有很多方法都标有删除线,是因为Date类在设计中有很多问题,如getYear指的是1900年以来的年数,getMonth是从0开始的。事实上,不止Date类,Java的其实时间相关类都存在设计问题,以下举些例子,并提供解决方案。

我们通常使用 Date和Calander用作时间处理,其实会有两个问题:
1.Date的缺陷,我们知道 Date的setYear和getYear等函数是删除线显示的
原因在:比如今天是2009-01-04日,那么获取的年竟然是109,所以是有问题的

2.Calender常常用于时间的回卷,经常使用的就是roll(Day_of_Year,-7)就是七天前
但是如果是2009-01-04日,那么七天前是2009-12-28日,而非2008年,这是因为它只对天回卷了,年没有回卷

3、针对这些问题,提供一套日期工具类:
import org.apache.log4j.Logger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class AdDateUtil {
private static Logger logger = Logger.getLogger(AdDateUtil.class);

static public String getNowStr(String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
String now = sdf.format(new Date());

return now;
}

static public Date getFormatDate(String date, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date d = new Date();

try {
d = sdf.parse(date);
} catch (ParseException e) {
logger.error(e);
}

return d;
}

static public String getDateStr(Date date, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
String d = sdf.format(date);

return d;
}

static public String getPadZeroString(String s, int size) {
StringBuffer sb = new StringBuffer();

for (int i = 0; i < (size - s.length()); i++) {
sb.append("0");
}

sb.append(s);

return sb.toString();
}

/**
* 得到某月的天数
*
* @param year
* @param month
* @return
*/
static public int getDayCountOfMonth(String year, String month) {
Calendar cal = Calendar.getInstance();
// 年
cal.set(Calendar.YEAR, Integer.parseInt(year));
// 月,因为Calendar里的月是从0开始,所以要-1
cal.set(Calendar.MONTH, Integer.parseInt(month) - 1);

return cal.getActualMaximum(Calendar.DAY_OF_MONTH);
}

static public String getYesterday(String format) {
SimpleDateFormat df = new SimpleDateFormat(format);

Calendar now = Calendar.getInstance();
now.roll(Calendar.DAY_OF_YEAR, -1); //昨天

return df.format(now.getTime());
}

/**
* 获取和今天附近的某天
* @param format
* @param diff
* @return
*/
static public String getADay(String format, int diff) {
SimpleDateFormat df = new SimpleDateFormat(format);

Calendar now = Calendar.getInstance();
int beforeM = now.get(Calendar.MONTH);
now.roll(Calendar.DAY_OF_YEAR, diff); //

int nowM = now.get(Calendar.MONTH);

//必须进行日期处理,否则2009-01-04日前七天是2009-12-28
if (nowM > beforeM) {
now.roll(Calendar.YEAR, -1);
}

return df.format(now.getTime());
}

static public String getTomorrow(String format) {
SimpleDateFormat df = new SimpleDateFormat(format);

Calendar now = Calendar.getInstance();
now.roll(Calendar.DAY_OF_YEAR, 1); //明天

return df.format(now.getTime());
}

/**
* 得到最近num天的全部日期
* 说明:
* 1.日期是从昨天开始算的.
* 2.如果num=2 , 日期是2008-03-14 ,则返回的结果为 2008-03-12、2008-03-13
* @param num
* @return
*/
public static String[] getDaysByNum(int num, String date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String[] result = { };
Calendar cal = Calendar.getInstance();
cal.setTime(getDateFromString(date, "yyyy-MM-dd"));

//最近一周
result = new String[num];

for (int i = num; i > 0; i--) {
cal.add(Calendar.DAY_OF_YEAR, -1);
result[i - 1] = sdf.format(new Date(cal.getTimeInMillis()));
}

return result;
}

public static Date getDateFromString(String dateStr, String format) {
if ((dateStr == null) || (format == null)) {
try {
throw new Exception("数据类型异常" + dateStr + "|" + format);
} catch (Exception e) {
logger.error("数据类型异常:" + e);
}
}

SimpleDateFormat df = new SimpleDateFormat(format);
Date date;

try {
date = df.parse(dateStr);

return date;
} catch (Exception ex) {
logger.error(ex);

return new Date();
}
}

static public int getNowYear() {
Calendar cal = Calendar.getInstance();

return cal.get(Calendar.YEAR);
}

static public int getNowMonth() {
Calendar cal = Calendar.getInstance();

return cal.get(Calendar.MONTH) + 1;
}

public static String[] getMonthRang(String year, String month) {
String beginDate = year + "-" + month + "-01";
String endDate = year + "-" + month + "-" +
getDayCountOfMonth(year, month);

return getDaysByRang(beginDate, endDate);
}

public static String[] getDaysByRang(String beginDate, String endDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

//得到两个日期间相差多少天
int num = dateDiff(beginDate, endDate);

if (num < 0) {
//颠倒一下日期
String tmp = beginDate;
beginDate = endDate;
endDate = tmp;
num = 0 - num;
}

String[] result = { };
Calendar cal = Calendar.getInstance();

try {
cal.setTime(sdf.parse(beginDate));
} catch (ParseException e) {
e.printStackTrace();
}

num = num + 1; //把开始和结束日期都包含进去

result = new String[num];

for (int i = 0; i < num; i++) {
if (i > 0) {
cal.add(Calendar.DAY_OF_YEAR, 1);
}

result[i] = sdf.format(new Date(cal.getTimeInMillis()));
}

return result;
}

public static int dateDiff(String beginDate, String endDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;

try {
date = sdf.parse(endDate);
} catch (ParseException e) {
date = new Date();
e.printStackTrace();
}

long end = date.getTime();

try {
date = sdf.parse(beginDate);
} catch (ParseException e) {
date = new Date();
e.printStackTrace();
}

long begin = date.getTime();

long day = (end - begin) / (1000 * 3600 * 24); //除1000是把毫秒变成秒

return Integer.parseInt(Long.toString(day));
}

public static void main(String[] args) {
System.out.println(AdDateUtil.getADay("yyyy-MM-dd", -7));
}
}

Ⅷ java Date类型。

Data类型是日期类型,通常是为了获取某些特定的日期或者转换日期的格式为字符串。举例:
Date date = new Date();//定义一个当前日期,此时输出就是日期类型
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//创建格式日期类型
String str = sdf.format(date);//对创建的日期进行格式化输出。此时输出就是字符串日期类型
输出结果:2015--6-30 19:17:32。

Ⅸ java date格式

你好:这个是你没保存的事,你保存下,然后关掉这个页面,在重新进来就没问题,他的编译有问题

热点内容
上传汽车保单 发布:2024-09-20 20:30:46 浏览:435
樱花服务器测试ip 发布:2024-09-20 20:10:39 浏览:279
炉石传说安卓怎么玩 发布:2024-09-20 20:09:59 浏览:312
ios开会员为什么比安卓贵 发布:2024-09-20 20:09:55 浏览:568
缓存服务器redis 发布:2024-09-20 20:09:01 浏览:75
优酷上传ts 发布:2024-09-20 19:55:58 浏览:273
minecraft怎么开服务器地址 发布:2024-09-20 19:52:14 浏览:651
android弹出布局 发布:2024-09-20 19:14:29 浏览:981
预算法包括 发布:2024-09-20 18:52:07 浏览:764
什么数字后面跟着密码 发布:2024-09-20 18:52:07 浏览:879