导航:首页 > 编程语言 > java获取日期月份

java获取日期月份

发布时间:2022-07-09 12:28:58

java 怎么获取一个时间的年月日

java获取一个时间的年月日代码及相关解释说明参考下面代码
package;
importjava.util.Calendar;
publicclassTest{
publicstaticvoidmain(String[]args){
Calendarcal=Calendar.getInstance();//使用日历类
intyear=cal.get(Calendar.YEAR);//获取年份
intmonth=cal.get(Calendar.MONTH)+1;//获取月份,因为从0开始的,所以要加1
intday=cal.get(Calendar.DAY_OF_MONTH);//获取天
System.out.println("结果:"+year+"-"+month+"-"+day);
}
}

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

(2)java获取日期月份扩展阅读

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,获得一个范围日期里面的每一个月份

可以通过日期的add(Calendar.MONTH, 1)方法进行月份切换,输出每个满足条件的值

Date d1 = new SimpleDateFormat("yyyy-MM").parse("2015-6");//定义起始日期

Date d2 = new SimpleDateFormat("yyyy-MM").parse("2016-5");//定义结束日期

Calendar dd = Calendar.getInstance();//定义日期实例

dd.setTime(d1);//设置日期起始时间

while(dd.getTime().before(d2)){//判断是否到结束日期

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

String str = sdf.format(dd.getTime());

System.out.println(str);//输出日期结果

dd.add(Calendar.MONTH, 1);//进行当前日期月份加1

}

结果:

❹ java如何获取当前时间 年月日 时分秒

//得到long类型当前时间

longl=System.currentTimeMillis();

//new日期对

Datedate=newDate(l);

//转换提日期输出格式

SimpleDateFormatdateFormat=newSimpleDateFormat("yyyy-MM-

ddHH:mm:ss");System.out.println(dateFormat.format(date));

(4)java获取日期月份扩展阅读

package com.ob;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

public class DateTest {

public static void main(String[] args) throws ParseException {

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

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

System.out.println("当前时间毫秒数:" + now.getTimeInMillis());

System.out.println(now.getTime());

Date d = new Date();

System.out.println(d);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateNowStr = sdf.format(d);

System.out.println("格式化后的日期:" + dateNowStr);

String str = "2012-1-13 17:26:33";

//要跟上面sdf定义的格式一样
Date today = sdf.parse(str);

System.out.println("字符串转成日期:" + today);
}
}

❺ 在JAVA中怎么获取当前时间的月份。并转换成int型

在JAVA中获取当前时间的月份并转换成int型可以采用Calendar类提供的方法进行。

具体代码如下:

Calendarcalendar=Calendar.getInstance();
//获得当前时间的月份,月份从0开始所以结果要加1
intmonth=calendar.get(Calendar.MONTH)+1;

❻ java中从数据库中读出日期类型后怎么提取年月或者日

直接通过格式转换的形式即可。举例:

String str0 = "2015年07月05日";

Date d1 = new SimpleDateFormat("yyyy年MM月dd日").parse(str0);//定义起始日期

SimpleDateFormat sdf0 = new SimpleDateFormat("yyyy");

SimpleDateFormat sdf1 = new SimpleDateFormat("MM");

SimpleDateFormat sdf2= new SimpleDateFormat("dd");

String str1 = sdf0.format(d1);

String str2 = sdf1.format(d1);

String str3 = sdf2.format(d1);

System.out.println("年份为:"+str1);

System.out.println("月份为:"+str2);

System.out.println("日为:"+str3);

结果:

❼ 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语句怎么把日期(类型为DATA)中的月份提取出来

Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
int month = cal.get(Calendar.MONTH); //注意月份是从0开始的,比如当前7月,获得的month为6

现在Date下面的大部分方法已经废弃,不推荐使用。

❾ java取日期中月份和日

Date date=new Date();
//获取月份
System.out.println(date.getMonth()+1);
//获取日
System.out.println(date.getDate());

❿ java 怎么获取一个月的日期

	/**
*
*获取指定月份的日历信息
*
*@paramyear
*年
*@parammonth
*月
*@return
*/
publicstaticint[]getMonthCalendar(intyear,intmonth){

Calendarcl=Calendar.getInstance();
cl.set(year,month,1);
intfirstDay=cl.getMinimum(Calendar.DAY_OF_MONTH);
intlastDay=cl.getMaximum(Calendar.DAY_OF_MONTH);

int[]day=newint[lastDay];

for(inti=0;i<lastDay;i++){
day[i]=i+firstDay;
}

returnday;
}

阅读全文

与java获取日期月份相关的资料

热点内容
不能修改的pdf 浏览:736
同城公众源码 浏览:474
一个服务器2个端口怎么映射 浏览:282
java字符串ascii码 浏览:59
台湾云服务器怎么租服务器 浏览:460
旅游手机网站源码 浏览:315
android关联表 浏览:929
安卓导航无声音怎么维修 浏览:320
app怎么装视频 浏览:423
安卓系统下的软件怎么移到桌面 浏览:80
windows拷贝到linux 浏览:753
mdr软件解压和别人不一样 浏览:886
单片机串行通信有什么好处 浏览:324
游戏开发程序员书籍 浏览:848
pdf中图片修改 浏览:275
汇编编译后 浏览:478
php和java整合 浏览:833
js中执行php代码 浏览:447
国产单片机厂商 浏览:62
苹果手机怎么设置不更新app软件 浏览:289