导航:首页 > 编程语言 > javadate月份

javadate月份

发布时间:2022-04-24 19:59:51

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取得年,月,日,时,分,秒

这个问题可以用两种方式得到:

方法一:在java中可以使用Date类直接获得,但是这个方法过时了,不推荐使用。

方法二:使用 java.util.Calendar 类。

代码例子:

//方法1:虽然还可以用,但是已经不建议使用,已经过时。
Datedate=newDate();
intold_y=date.getYear()+1900;//得到年份。因为得到的是1900年后至今过了多少年,所以要加1900
intold_m=date.getMonth()+1;//因为得到的结果是0~11,故而加一。
intold_d=date.getDate();//得到月份中今天的号数
System.out.println("现在是:"+old_y+"-"+old_m+"-"+old_d+"(使用过时方法)");//

//方法2:推荐使用
Calendarcalendar=Calendar.getInstance();
intnow_y=calendar.get(Calendar.YEAR);//得到年份
intnow_m=calendar.get(Calendar.MONTH)+1;//得到月份
intnow_d=calendar.get(Calendar.DATE);//得到月份中今天的号数
intnow_h=calendar.get(Calendar.HOUR_OF_DAY);//得到一天中现在的时间,24小时制
intnow_mm=calendar.get(Calendar.MINUTE);//得到分钟数
intnow_s=calendar.get(Calendar.SECOND);//得到秒数
System.out.println("现在是:"+now_y+"-"+now_m+"-"+now_d+""+now_h+":"+now_mm+":"+now_s+"(使用推荐方法)");

结果:

现在是:2015-11-9(使用过时方法)

现在是:2015-11-9 18:7:42(使用推荐方法)

⑶ java中如何从日期类型中获取月

//将yyyyMMdd转为date
public static Date getCoreToEmsDateStr(String dateStr){

DateFormat format = new SimpleDateFormat("yyyyMMdd");
Date d = null;
try{
d = format.parse(dateStr);
}catch(ParseException e){
e.printStackTrace();
}
return d;
}

public static String getDateAfterDays(Timestamp s,int days){
Timestamp currTimestamp = s;
for (int i=0;i<days;i++){
currTimestamp = getNextDate(currTimestamp);
}
return getDateTimeStr(currTimestamp,"3");
}

public static Timestamp getNextDate(java.sql.Timestamp tsDate){
if(tsDate==null)
return null;
java.util.Calendar calendar = Calendar.getInstance();
calendar.setTime(tsDate);
return getDateTime(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH)+1,calendar.get(Calendar.DATE)+1,
calendar.get(Calendar.HOUR_OF_DAY),calendar.get(Calendar.MINUTE),calendar.get(Calendar.SECOND));
}

public static java.sql.Timestamp getDateTime(int year,int month,int day,int hour,int minute,int second){
java.sql.Timestamp ts = null;
java.util.Date dt = null;
java.util.Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(year,month-1,day,hour,minute,second);
dt = calendar.getTime();
ts = new java.sql.Timestamp(dt.getTime());

return ts;
}

/**
* 比较两个时间是否相同
* @param tsBeginDate
* @param tsEndDate
* @param bool
* @return
*/
public static long getDateInterval(Timestamp tsBeginDate,Timestamp tsEndDate,boolean bool){
long lDays = 0;
if(bool){
tsBeginDate = Common.getDateTime(Common.getDateString(tsBeginDate),bool);
}
if(tsBeginDate!=null&&tsEndDate!=null){
Log4j.info("tsEndDate.getTime ()===="+tsEndDate);
Log4j.info("tsBeginDate.getTime ()===="+tsBeginDate);
lDays = (tsEndDate.getTime()-tsBeginDate.getTime())/86400000+1;
Log4j.info("lDays===="+lDays);
}

return lDays;
}

/**
* 格式化成Timestamp类型
* @param sDt
* @param bool
* @return
*/
public static java.sql.Timestamp getDateTime(String sDt,boolean bool){
try{
return java.sql.Timestamp.valueOf(sDt); //sDt format:yyyy-mm-dd hh:mm:ss.fffffffff
}catch(IllegalArgumentException iae){
if(bool)
sDt = sDt+" 23:59:59.0";
else
sDt = sDt+" 00:00:00.0";
return java.sql.Timestamp.valueOf(sDt);
}
}
/**
* 根据时间获取日期字符串

* @param ts
* @return
*/
public static String getDateString(Timestamp ts){
if(ts==null)
return "";
Calendar calendar = Calendar.getInstance();
calendar.setTime(ts);
String strMonth = String.valueOf(calendar.get(Calendar.MONTH)+1);
if(strMonth.length()==1){
strMonth = "0"+strMonth;
}
String strDay = String.valueOf(calendar.get(Calendar.DATE));
if(strDay.length()==1){
strDay = "0"+strDay;
}
return calendar.get(Calendar.YEAR)+"-"+strMonth+"-"+strDay;
}

⑷ Java中如何设置Date对象的年月日

Date
public Date(int year,
int month,
int day)
参数:
year - year 减去 1900,它必须是 0 到 8099 之间的数。(注意,8099 是由 9999 减去 1900 得到的。)
month - 0 到 11 之间的数
day - 1 到 31 之间的数

测试代码如下:
import java.util.Date;

public class Test {
public static void main(String args[]){
Date date = new Date(2010-1900,1,10);
System.out.println(date);
}
}

运行结果:
Wed Feb 10 00:00:00 CST 2010

希望对你有帮助。。。。。。仍有问题可以HI我。。。。

⑸ java的date类,得到两位月份的方法如01月

java为了支持多语言,没有固定的日期格式。你需要根据自己的需要指定日期格式,然后用dateformat类或者simpledateformat类来判断是否是正确的日期格式。下面的例子供参考。更详细的内容(比如yyyy,mm,dd各代表什么)可以参考javadoc。
public
class
dateutil
{
private
static
final
simpledateformat
dateformat
=
null;
static
{
//
指定日期格式为四位年/两位月份/两位日期,注意yyyy/mm/dd区分大小写;
dateformat
=
new
simpledateformat("yyyy/mm/dd");
//
设置lenient为false.
否则simpledateformat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01
dateformat.setlenient(false);
}
public
static
boolean
isvaliddate(string
s)
{
try
{
dateformat.parse(s);
return
true;
}
catch
(exception
e)
{
//
如果throw
java.text.parseexception或者nullpointerexception,就说明格式不对
return
false;
}
}
//
下面这个方法则可以将一个日期按照你指定的格式输出
public
static
string
formatdate(date
d)
{
return
dateformat.format(d);
}
}

⑹ 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如何得到年月日。

1、获取当前的时间

Date date=new Date();//此时date为当前的时间

2、设置时间的格式

Date date=new Date();//此时date为当前的时间

System.out.println(date);

SimpleDateFormat dateFormat=new SimpleDateFormat(“YYYY-MM-dd”);//设置当前时间的格式,为年-月-日

System.out.println(dateFormat.format(date));

SimpleDateFormat dateFormat_min=new SimpleDateFormat(“YYYY-MM-dd HH:mm:ss”);//设置当前时间的格式,为年-月-日 时-分-秒

System.out.println(dateFormat_min.format(date));

(7)javadate月份扩展阅读

java 获取当前微秒时间:

package com.ffcs.itm;

public class DataSecUtils {

public static void main(String[] args) {

System.out.println(System.currentTimeMillis()); // 毫秒

System.out.println(getmicTime());

System.out.println(System.currentTimeMillis()); // 毫秒

System.out.println(getmicTime());

}

/**

* @return返回微秒

*/

public static Long getmicTime() {

Long cutime = System.currentTimeMillis() * 1000; // 微秒

Long nanoTime = System.nanoTime(); // 纳秒

return cutime + (nanoTime - nanoTime / 1000000 * 1000000) / 1000;

}

}

⑻ java Date类型 按年,月,日 取出并输出,怎么编写

这是根据你的要求编写的代码。

import java.util.Calendar;

import java.sql.Date;

public class TestAA {

/**

* @param args

*/

public static void main(String[] args) {

Calendar cld = Calendar.getInstance();

Date date = new java.sql.Date(1319534374312l);;

cld.setTime(date);

/**

* 注:在jdk1.6以后下列方法都已过时

* date.getYear();

* date.getMonth();

* date.getDay();

*/

System.out.println("日期为:"+date.toString());

//方法一

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

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

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

//方法er

System.out.println("年份:"+date.toString().split("-")[0]);

System.out.println("月份:"+date.toString().split("-")[1]);

System.out.println("日:"+date.toString().split("-")[2]);

}

}

你看看,应该没问题。

⑼ java Date类型 按年,月,日 取出并输出 求高手.

很高兴回答你的问题
下边是根据你的需求写的代码:

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

public class TestAA {

/**
* @param args
*/
public static void main(String[] args) {
Calendar cld = Calendar.getInstance();
Date date = new java.sql.Date(1319534374312l);;
cld.setTime(date);
/**
* 注:在jdk1.6以后下列方法都已过时
* date.getYear();
* date.getMonth();
* date.getDay();
*/
System.out.println("日期为:"+date.toString());
//方法一
System.out.println("年份:"+cld.get(Calendar.YEAR));
System.out.println("月份:"+(cld.get(Calendar.MONTH)+1));
System.out.println("日:"+cld.get(Calendar.DAY_OF_MONTH));
//方法er
System.out.println("年份:"+date.toString().split("-")[0]);
System.out.println("月份:"+date.toString().split("-")[1]);
System.out.println("日:"+date.toString().split("-")[2]);
}

}

如有疑问,请Hi我,谢谢!
祝学习愉快!

⑽ JAVA语句怎么把日期(类型为DATA)中的月份提取出来

Calendar
cal
=
Calendar.getInstance();
cal.setTime(new
Date());
int
month
=
cal.get(Calendar.MONTH);
//注意月份是从0开始的,比如当前7月,获得的month为6
现在Date下面的大部分方法已经废弃,不推荐使用。

阅读全文

与javadate月份相关的资料

热点内容
亚马逊云服务器查看 浏览:163
如何用免费云服务器 浏览:610
php的输出命令 浏览:264
在家怎么制作解压小玩具 浏览:99
javascript源码辅助阅读 浏览:384
pythonui开发工具 浏览:595
adr指标源码 浏览:217
程序员转架构管理 浏览:958
企业服务器为什么不能被拷贝 浏览:119
用c编程实现txt搜索 浏览:854
JAVA小数随机数 浏览:337
app加拿大pc怎么操控的 浏览:698
光影app苹果怎么下载不了 浏览:971
php会员注册代码 浏览:511
csgo如何用128tick服务器 浏览:571
百度网度怎么解压 浏览:946
windowsopencv源码 浏览:946
origin平滑算法 浏览:875
unity程序员简历 浏览:63
单片机ifelse 浏览:696