java获取date类型的年份
在java编程中,我们经常会涉及到日期和时间的操作。而获取一个date类型对象的年份是其中的一个常见需求。本文将介绍如何使用java获取date类型的年份,并提供相应的代码示例。
date类简介
在java中,date类是用于表示日期和时间的类。它提供了一些方法来获取和设置日期和时间的各个部分,比如年份、月份、日等。
获取date对象的年份
要获取一个date对象的年份,可以使用date类中的getyear()方法。但需要注意的是,该方法返回的年份是相对于1900年的偏移量。例如,返回值为120表示的是2020年。
以下是一个示例代码,演示如何获取date对象的年份:
import java.util.date;
public class getyearexample {
public static void main(string[] args) {
// 创建一个date对象
date date = new date();
// 获取年份
int year = date.getyear() + 1900;
// 输出年份
system.out.println("当前年份:" + year);
}
}
在上述代码中,我们首先创建了一个date对象,然后调用了getyear()方法获取年份。由于getyear()方法返回的是相对于1900年的偏移量,所以我们需要将其加上1900,以得到实际的年份。
使用calendar类获取年份
除了使用date类的getyear()方法外,还可以使用calendar类来获取date对象的年份。calendar类提供了更多的日期和时间操作方法。
以下是使用calendar类获取date对象年份的示例代码:
import java.util.calendar;
import java.util.date;
public class getyearexample {
public static void main(string[] args) {
// 创建一个calendar对象
calendar calendar = calendar.getinstance();
// 设置calendar对象的时间为当前时间
calendar.settime(new date());
// 获取年份
int year = calendar.get(calendar.year);
// 输出年份
system.out.println("当前年份:" + year);
}
}
在上述代码中,我们首先创建了一个calendar对象,并使用getinstance()方法获取了当前的实例。然后,通过调用settime()方法,将calendar对象的时间设置为当前时间。最后,使用get()方法并传入calendar.year参数,即可获取年份。
总结
通过使用date类的getyear()方法或使用calendar类的get(calendar.year)方法,我们可以方便地获取一个date对象的年份。在实际开发中,我们可以根据具体的需求选择合适的方法来获取年份。
流程图如下所示:
flowchart td
start --> 创建一个date对象
创建一个date对象 --> 调用getyear()方法获取年份
调用getyear()方法获取年份 --> 输出年份
start --> 创建一个calendar对象
创建一个calendar对象 --> 设置时间为当前时间
设置时间为当前时间 --> 调用get(calendar.year)方法获取年份
调用get(calendar.year)方法获取年份 --> 输出年份
附:其他的获取时间的方式
package com.qunar.date;
import java.text.parseexception;
import java.text.parseposition;
import java.text.simpledateformat;
import java.util.*;
/**
* created by liugen.xu on 2015/8/4.
*/
public class dategenerate {
/**
* 获取现在时间
*
* @return 返回时间类型 yyyy-mm-dd hh:mm:ss
*/
public static date getnowdate() {
date currenttime = new date();
simpledateformat formatter = new simpledateformat("yyyy-mm-dd hh:mm:ss");
string datestring = formatter.format(currenttime);
parseposition pos = new parseposition(8);
date currenttime_2 = formatter.parse(datestring, pos);
return currenttime_2;
}
/**
* 获取现在时间
*
* @return返回短时间格式 yyyy-mm-dd
*/
public static date getnowdateshort() {
date currenttime = new date();
simpledateformat formatter = new simpledateformat("yyyy-mm-dd");
string datestring = formatter.format(currenttime);
parseposition pos = new parseposition(8);
date currenttime_2 = formatter.parse(datestring, pos);
return currenttime_2;
}
/**
* 获取现在时间
*
* @return返回字符串格式 yyyy-mm-dd hh:mm:ss
*/
public static string getstringdate() {
date currenttime = new date();
simpledateformat formatter = new simpledateformat("yyyy-mm-dd hh:mm:ss");
string datestring = formatter.format(currenttime);
return datestring;
}
/**
* 获取现在时间
*
* @return 返回短时间字符串格式yyyy-mm-dd
*/
public static string getstringdateshort() {
date currenttime = new date();
simpledateformat formatter = new simpledateformat("yyyy-mm-dd");
string datestring = formatter.format(currenttime);
return datestring;
}
/**
* 获取时间 小时:分;秒 hh:mm:ss
*
* @return
*/
public static string gettimeshort() {
simpledateformat formatter = new simpledateformat("hh:mm:ss");
date currenttime = new date();
string datestring = formatter.format(currenttime);
return datestring;
}
/**
* 将长时间格式字符串转换为时间 yyyy-mm-dd hh:mm:ss
*
* @param strdate
* @return
*/
public static date strtodatelong(string strdate) {
simpledateformat formatter = new simpledateformat("yyyy-mm-dd hh:mm:ss");
parseposition pos = new parseposition(0);
date strtodate = formatter.parse(strdate, pos);
return strtodate;
}
/**
* 将长时间格式时间转换为字符串 yyyy-mm-dd hh:mm:ss
*
* @param datedate
* @return
*/
public static string datetostrlong(java.util.date datedate) {
simpledateformat formatter = new simpledateformat("yyyy-mm-dd hh:mm:ss");
string datestring = formatter.format(datedate);
return datestring;
}
/**
* 将短时间格式时间转换为字符串 yyyy-mm-dd
*
* @param datedate
* @return
*/
public static string datetostr(java.util.date datedate) {
simpledateformat formatter = new simpledateformat("yyyy-mm-dd");
string datestring = formatter.format(datedate);
return datestring;
}
/**
* 将短时间格式字符串转换为时间 yyyy-mm-dd
*
* @param strdate
* @return
*/
public static date strtodate(string strdate) {
simpledateformat formatter = new simpledateformat("yyyy-mm-dd");
parseposition pos = new parseposition(0);
date strtodate = formatter.parse(strdate, pos);
return strtodate;
}
/**
* 得到现在时间
*
* @return
*/
public static date getnow() {
date currenttime = new date();
return currenttime;
}
/**
* 提取一个月中的最后一天
*
* @param day
* @return
*/
public static date getlastdate(long day) {
date date = new date();
long date_3_hm = date.gettime() - 3600000 * 34 * day;
date date_3_hm_date = new date(date_3_hm);
return date_3_hm_date;
}
/**
* 得到现在时间
*
* @return 字符串 yyyymmdd hhmmss
*/
public static string getstringtoday() {
date currenttime = new date();
simpledateformat formatter = new simpledateformat("yyyymmdd hhmmss");
string datestring = formatter.format(currenttime);
return datestring;
}
/**
* 得到现在小时
*/
public static string gethour() {
date currenttime = new date();
simpledateformat formatter = new simpledateformat("yyyy-mm-dd hh:mm:ss");
string datestring = formatter.format(currenttime);
string hour;
hour = datestring.substring(11, 13);
return hour;
}
/**
* 得到现在分钟
*
* @return
*/
public static string gettime() {
date currenttime = new date();
simpledateformat formatter = new simpledateformat("yyyy-mm-dd hh:mm:ss");
string datestring = formatter.format(currenttime);
string min;
min = datestring.substring(14, 16);
return min;
}
/**
* 根据用户传入的时间表示格式,返回当前时间的格式 如果是yyyymmdd,注意字母y不能大写。
*
* @param sformat yyyymmddhhmmss
* @return
*/
public static string getuserdate(string sformat) {
date currenttime = new date();
simpledateformat formatter = new simpledateformat(sformat);
string datestring = formatter.format(currenttime);
return datestring;
}
/**
* 二个小时时间间的差值,必须保证二个时间都是"hh:mm"的格式,返回字符型的分钟
*/
public static string gettwohour(string st1, string st2) {
string[] kk = null;
string[] jj = null;
kk = st1.split(":");
jj = st2.split(":");
if (integer.parseint(kk[0]) < integer.parseint(jj[0]))
return "0";
else {
double y = double.parsedouble(kk[0]) + double.parsedouble(kk[1]) / 60;
double u = double.parsedouble(jj[0]) + double.parsedouble(jj[1]) / 60;
if ((y - u) > 0)
return y - u + "";
else
return "0";
}
}
/**
* 得到二个日期间的间隔天数
*/
public static string gettwoday(string sj1, string sj2) {
simpledateformat myformatter = new simpledateformat("yyyy-mm-dd");
long day = 0;
try {
java.util.date date = myformatter.parse(sj1);
java.util.date mydate = myformatter.parse(sj2);
day = (date.gettime() - mydate.gettime()) / (24 * 60 * 60 * 1000);
} catch (exception e) {
return "";
}
return day + "";
}
/**
* 时间前推或后推分钟,其中jj表示分钟.
*/
public static string getpretime(string sj1, string jj) {
simpledateformat format = new simpledateformat("yyyy-mm-dd hh:mm:ss");
string mydate1 = "";
try {
date date1 = format.parse(sj1);
long time = (date1.gettime() / 1000) + integer.parseint(jj) * 60;
date1.settime(time * 1000);
mydate1 = format.format(date1);
} catch (exception e) {
}
return mydate1;
}
/**
* 得到一个时间延后或前移几天的时间,nowdate为时间,delay为前移或后延的天数
*/
public static string getnextday(string nowdate, string delay) {
try {
simpledateformat format = new simpledateformat("yyyy-mm-dd");
string mdate = "";
date d = strtodate(nowdate);
long mytime = (d.gettime() / 1000) + integer.parseint(delay) * 24 * 60 * 60;
d.settime(mytime * 1000);
mdate = format.format(d);
return mdate;
} catch (exception e) {
return "";
}
}
/**
* 判断是否润年
*
* @param ddate
* @return
*/
public static boolean isleapyear(string ddate) {
/**
* 详细设计: 1.被400整除是闰年,否则: 2.不能被4整除则不是闰年 3.能被4整除同时不能被100整除则是闰年
* 3.能被4整除同时能被100整除则不是闰年
*/
date d = strtodate(ddate);
gregoriancalendar gc = (gregoriancalendar) calendar.getinstance();
gc.settime(d);
int year = gc.get(calendar.year);
if ((year % 400) == 0)
return true;
else if ((year % 4) == 0) {
if ((year % 100) == 0)
return false;
else
return true;
} else
return false;
}
/**
* 返回美国时间格式 26 apr 2006
*
* @param str
* @return
*/
public static string getedate(string str) {
simpledateformat formatter = new simpledateformat("yyyy-mm-dd");
parseposition pos = new parseposition(0);
date strtodate = formatter.parse(str, pos);
string j = strtodate.tostring();
string[] k = j.split(" ");
return k[2] + k[1].touppercase() + k[5].substring(2, 4);
}
/**
* 获取一个月的最后一天
*
* @param dat
* @return
*/
public static string getenddateofmonth(string dat) {// yyyy-mm-dd
string str = dat.substring(0, 8);
string month = dat.substring(5, 7);
int mon = integer.parseint(month);
if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10 || mon == 12) {
str += "31";
} else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {
str += "30";
} else {
if (isleapyear(dat)) {
str += "29";
} else {
str += "28";
}
}
return str;
}
/**
* 判断二个时间是否在同一个周
*
* @param date1
* @param date2
* @return
*/
public static boolean issameweekdates(date date1, date date2) {
calendar cal1 = calendar.getinstance();
calendar cal2 = calendar.getinstance();
cal1.settime(date1);
cal2.settime(date2);
int subyear = cal1.get(calendar.year) - cal2.get(calendar.year);
if (0 == subyear) {
if (cal1.get(calendar.week_of_year) == cal2.get(calendar.week_of_year))
return true;
} else if (1 == subyear && 11 == cal2.get(calendar.month)) {
// 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周
if (cal1.get(calendar.week_of_year) == cal2.get(calendar.week_of_year))
return true;
} else if (-1 == subyear && 11 == cal1.get(calendar.month)) {
if (cal1.get(calendar.week_of_year) == cal2.get(calendar.week_of_year))
return true;
}
return false;
}
/**
* 产生周序列,即得到当前时间所在的年度是第几周
*
* @return
*/
public static string getseqweek() {
calendar c = calendar.getinstance(locale.china);
string week = integer.tostring(c.get(calendar.week_of_year));
if (week.length() == 1)
week = "0" + week;
string year = integer.tostring(c.get(calendar.year));
return year + week;
}
/**
* 获得一个日期所在的周的星期几的日期,如要找出2002年2月3日所在周的星期一是几号
*
* @param sdate
* @param num
* @return
*/
public static string getweek(string sdate, string num) {
// 再转换为时间
date dd = dategenerate.strtodate(sdate);
calendar c = calendar.getinstance();
c.settime(dd);
if (num.equals("1")) // 返回星期一所在的日期
c.set(calendar.day_of_week, calendar.monday);
else if (num.equals("2")) // 返回星期二所在的日期
c.set(calendar.day_of_week, calendar.tuesday);
else if (num.equals("3")) // 返回星期三所在的日期
c.set(calendar.day_of_week, calendar.wednesday);
else if (num.equals("4")) // 返回星期四所在的日期
c.set(calendar.day_of_week, calendar.thursday);
else if (num.equals("5")) // 返回星期五所在的日期
c.set(calendar.day_of_week, calendar.friday);
else if (num.equals("6")) // 返回星期六所在的日期
c.set(calendar.day_of_week, calendar.saturday);
else if (num.equals("0")) // 返回星期日所在的日期
c.set(calendar.day_of_week, calendar.sunday);
return new simpledateformat("yyyy-mm-dd").format(c.gettime());
}
/**
* 根据一个日期,返回是星期几的字符串
*
* @param sdate
* @return
*/
public static string getweek(string sdate) {
// 再转换为时间
date date = dategenerate.strtodate(sdate);
calendar c = calendar.getinstance();
c.settime(date);
// int hour=c.get(calendar.day_of_week);
// hour中存的就是星期几了,其范围 1~7
// 1=星期日 7=星期六,其他类推
return new simpledateformat("eeee").format(c.gettime());
}
public static string getweekstr(string sdate) {
string str = "";
str = dategenerate.getweek(sdate);
if ("1".equals(str)) {
str = "星期日";
} else if ("2".equals(str)) {
str = "星期一";
} else if ("3".equals(str)) {
str = "星期二";
} else if ("4".equals(str)) {
str = "星期三";
} else if ("5".equals(str)) {
str = "星期四";
} else if ("6".equals(str)) {
str = "星期五";
} else if ("7".equals(str)) {
str = "星期六";
}
return str;
}
/**
* 两个时间之间的天数
*
* @param date1
* @param date2
* @return
*/
public static long getdays(string date1, string date2) {
if (date1 == null || date1.equals(""))
return 0;
if (date2 == null || date2.equals(""))
return 0;
// 转换为标准时间
simpledateformat myformatter = new simpledateformat("yyyy-mm-dd");
java.util.date date = null;
java.util.date mydate = null;
try {
date = myformatter.parse(date1);
mydate = myformatter.parse(date2);
} catch (exception e) {
}
long day = (date.gettime() - mydate.gettime()) / (24 * 60 * 60 * 1000);
return day;
}
/**
* 形成如下的日历 , 根据传入的一个时间返回一个结构 星期日 星期一 星期二 星期三 星期四 星期五 星期六 下面是当月的各个时间
* 此函数返回该日历第一行星期日所在的日期
*
* @param sdate
* @return
*/
public static string getnowmonth(string sdate) {
// 取该时间所在月的一号
sdate = sdate.substring(0, 8) + "01";
// 得到这个月的1号是星期几
date date = dategenerate.strtodate(sdate);
calendar c = calendar.getinstance();
c.settime(date);
int u = c.get(calendar.day_of_week);
string newday = dategenerate.getnextday(sdate, (1 - u) + "");
return newday;
}
/**
* 取得数据库主键 生成格式为yyyymmddhhmmss+k位随机数
*
* @param k 表示是取几位随机数,可以自己定
*/
public static string getno(int k) {
return getuserdate("yyyymmddhhmmss") + getrandom(k);
}
/**
* 返回一个随机数
*
* @param i
* @return
*/
public static string getrandom(int i) {
random jjj = new random();
// int suijishu = jjj.nextint(9);
if (i == 0)
return "";
string jj = "";
for (int k = 0; k < i; k++) {
jj = jj + jjj.nextint(9);
}
return jj;
}
/**
*/
public static boolean rightdate(string date) {
simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss");
;
if (date == null)
return false;
if (date.length() > 10) {
sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss");
} else {
sdf = new simpledateformat("yyyy-mm-dd");
}
try {
sdf.parse(date);
} catch (parseexception pe) {
return false;
}
return true;
}
/**
* ************************************************************************
* //nd=1表示返回的值中包含年度 //yf=1表示返回的值中包含月份 //rq=1表示返回的值中包含日期 //format表示返回的格式 1
* 以年月日中文返回 2 以横线-返回 // 3 以斜线/返回 4 以缩写不带其它符号形式返回 // 5 以点号.返回
* ************************************************************************
*/
public static string getstringdatemonth(string sdate, string nd, string yf, string rq, string format) {
date currenttime = new date();
simpledateformat formatter = new simpledateformat("yyyy-mm-dd");
string datestring = formatter.format(currenttime);
string s_nd = datestring.substring(0, 4); // 年份
string s_yf = datestring.substring(5, 7); // 月份
string s_rq = datestring.substring(8, 10); // 日期
string sreturn = "";
roc.util.mychar mc = new roc.util.mychar();
if (sdate == null || sdate.equals("") || !mc.isdate(sdate)) { // 处理空值情况
if (nd.equals("1")) {
sreturn = s_nd;
// 处理间隔符
if (format.equals("1"))
sreturn = sreturn + "年";
else if (format.equals("2"))
sreturn = sreturn + "-";
else if (format.equals("3"))
sreturn = sreturn + "/";
else if (format.equals("5"))
sreturn = sreturn + ".";
}
// 处理月份
if (yf.equals("1")) {
sreturn = sreturn + s_yf;
if (format.equals("1"))
sreturn = sreturn + "月";
else if (format.equals("2"))
sreturn = sreturn + "-";
else if (format.equals("3"))
sreturn = sreturn + "/";
else if (format.equals("5"))
sreturn = sreturn + ".";
}
// 处理日期
if (rq.equals("1")) {
sreturn = sreturn + s_rq;
if (format.equals("1"))
sreturn = sreturn + "日";
}
} else {
// 不是空值,也是一个合法的日期值,则先将其转换为标准的时间格式
sdate = roc.util.rocdate.getokdate(sdate);
s_nd = sdate.substring(0, 4); // 年份
s_yf = sdate.substring(5, 7); // 月份
s_rq = sdate.substring(8, 10); // 日期
if (nd.equals("1")) {
sreturn = s_nd;
// 处理间隔符
if (format.equals("1"))
sreturn = sreturn + "年";
else if (format.equals("2"))
sreturn = sreturn + "-";
else if (format.equals("3"))
sreturn = sreturn + "/";
else if (format.equals("5"))
sreturn = sreturn + ".";
}
// 处理月份
if (yf.equals("1")) {
sreturn = sreturn + s_yf;
if (format.equals("1"))
sreturn = sreturn + "月";
else if (format.equals("2"))
sreturn = sreturn + "-";
else if (format.equals("3"))
sreturn = sreturn + "/";
else if (format.equals("5"))
sreturn = sreturn + ".";
}
// 处理日期
if (rq.equals("1")) {
sreturn = sreturn + s_rq;
if (format.equals("1"))
sreturn = sreturn + "日";
}
}
return sreturn;
}
public static string getnextmonthday(string sdate, int m) {
sdate = getokdate(sdate);
int year = integer.parseint(sdate.substring(0, 4));
int month = integer.parseint(sdate.substring(5, 7));
month = month + m;
if (month < 0) {
month = month + 12;
year = year - 1;
} else if (month > 12) {
month = month - 12;
year = year + 1;
}
string smonth = "";
if (month < 10)
smonth = "0" + month;
else
smonth = "" + month;
return year + "-" + smonth + "-10";
}
public static string getokdate(string sdate) {
if (sdate == null || sdate.equals(""))
return getstringdateshort();
if (!vestr.isdate(sdate)) {
sdate = getstringdateshort();
}
// 将“/”转换为“-”
sdate = vestr.replace(sdate, "/", "-");
// 如果只有8位长度,则要进行转换
if (sdate.length() == 8)
sdate = sdate.substring(0, 4) + "-" + sdate.substring(4, 6) + "-" + sdate.substring(6, 8);
simpledateformat formatter = new simpledateformat("yyyy-mm-dd");
parseposition pos = new parseposition(0);
date strtodate = formatter.parse(sdate, pos);
string datestring = formatter.format(strtodate);
return datestring;
}
public static void main(string[] args) throws exception {
try {
//system.out.print(integer.valueof(gettwoday("2006-11-03 12:22:10", "2006-11-02 11:22:09")));
} catch (exception e) {
throw new exception();
}
//system.out.println("sss");
}
}到此这篇关于java获取date类型年份的文章就介绍到这了,更多相关java获取date类型年份内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论