精华内容
下载资源
问答
  • * @param num 负数为之前年,0为当前年,正数为之后年 * @Throws * @Return java.lang.String * @Date 2021-09-06 10:28:15 * @Author WangKun **/ public static String GetYears(String pattern,..
     /**
         * @Description 获取年份
          * @param pattern 格式
         * @param num 负数为之前年,0为当前年,正数为之后年
         * @Throws
         * @Return java.lang.String
         * @Date 2021-09-06 10:28:15
         * @Author WangKun
         **/
        public static String GetYears(String pattern,int num) {
            SimpleDateFormat format = new SimpleDateFormat(pattern);
            Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            c.add(Calendar.YEAR, num);
            Date y = c.getTime();
            return format.format(y);
        }

    展开全文
  • /*** java 获取当前年份 月份 日期*/import java.util.Calendar;public class test {public static void main(String[] args) {//用CalendarCalendar cal = Calendar.getInstance();int day = cal.get(Calendar.DATE...

    /**

    * java 获取当前年份 月份 日期

    */

    import java.util.Calendar;

    public class test {

    public static void main(String[] args) {

    //用Calendar

    Calendar cal = Calendar.getInstance();

    int day = cal.get(Calendar.DATE);

    int month = cal.get(Calendar.MONTH) + 1;

    int year = cal.get(Calendar.YEAR);

    int dow = cal.get(Calendar.DAY_OF_WEEK);

    int dom = cal.get(Calendar.DAY_OF_MONTH);

    int doy = cal.get(Calendar.DAY_OF_YEAR);

    System.out.println("==============用Calendar=====================");

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

    System.out.println("Day: " + day);

    System.out.println("Month: " + month);

    System.out.println("Year: " + year);

    System.out.println("Day of Week: " + dow);

    System.out.println("Day of Month: " + dom);

    System.out.println("Day of Year: " + doy);

    //用Date

    java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    java.util.Date currTime = new java.util.Date();

    int year2 = currTime.getYear();//年

    int month2 = currTime.getMonth()+1;//月

    int dow2 = currTime.getDay(); //星期几

    int day2 = currTime.getDate();//日

    String curTime = formatter.format(currTime);

    System.out.println("==============用Date=====================");

    System.out.println("Current Date: " + currTime);

    System.out.println("Day: " + day2);

    System.out.println("Month: " + month2);

    System.out.println("Year: " + year2);

    System.out.println("Day of Week: " + dow2);

    }

    }

    展开全文
  • ps:代码有参考别人的,非全部原创。已在正式项目上使用import java.text.SimpleDateFormat.../*** 获取 当前年、半年、季度、月、日、小时 开始结束时间*/private final SimpleDateFormat shortSdf;private final Sim...

    ps:代码有参考别人的,非全部原创。已在正式项目上使用

    import java.text.SimpleDateFormat;

    import java.util.Calendar;

    import java.util.Date;

    /**

    * 获取  当前年、半年、季度、月、日、小时 开始结束时间

    */

    private final SimpleDateFormat shortSdf;

    private final SimpleDateFormat longHourSdf;

    private final SimpleDateFormat longSdf;

    public RemindDateUtils(){

    this.shortSdf = new SimpleDateFormat("yyyy-MM-dd");

    this.longHourSdf = new SimpleDateFormat("yyyy-MM-dd HH");

    this.longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    }

    /**

    * 获得本周的第一天,周一

    *

    * @return

    */

    public   Date getCurrentWeekDayStartTime() {

    Calendar c = Calendar.getInstance();

    try {

    int weekday = c.get(Calendar.DAY_OF_WEEK) - 2;

    c.add(Calendar.DATE, -weekday);

    c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00"));

    } catch (Exception e) {

    e.printStackTrace();

    }

    return c.getTime();

    }

    /**

    * 获得本周的最后一天,周日

    *

    * @return

    */

    public   Date getCurrentWeekDayEndTime() {

    Calendar c = Calendar.getInstance();

    try {

    int weekday = c.get(Calendar.DAY_OF_WEEK);

    c.add(Calendar.DATE, 8 - weekday);

    c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59"));

    } catch (Exception e) {

    e.printStackTrace();

    }

    return c.getTime();

    }

    /**

    * 获得本天的开始时间,即2012-01-01 00:00:00

    *

    * @return

    */

    public   Date getCurrentDayStartTime() {

    Date now = new Date();

    try {

    now = shortSdf.parse(shortSdf.format(now));

    } catch (Exception e) {

    e.printStackTrace();

    }

    return now;

    }

    /**

    * 获得本天的结束时间,即2012-01-01 23:59:59

    *

    * @return

    */

    public   Date getCurrentDayEndTime() {

    Date now = new Date();

    try {

    now = longSdf.parse(shortSdf.format(now) + " 23:59:59");

    } catch (Exception e) {

    e.printStackTrace();

    }

    return now;

    }

    /**

    * 获得本小时的开始时间,即2012-01-01 23:59:59

    *

    * @return

    */

    public   Date getCurrentHourStartTime() {

    Date now = new Date();

    try {

    now = longHourSdf.parse(longHourSdf.format(now));

    } catch (Exception e) {

    e.printStackTrace();

    }

    return now;

    }

    /**

    * 获得本小时的结束时间,即2012-01-01 23:59:59

    *

    * @return

    */

    public   Date getCurrentHourEndTime() {

    Date now = new Date();

    try {

    now = longSdf.parse(longHourSdf.format(now) + ":59:59");

    } catch (Exception e) {

    e.printStackTrace();

    }

    return now;

    }

    /**

    * 获得本月的开始时间,即2012-01-01 00:00:00

    *

    * @return

    */

    public   Date getCurrentMonthStartTime() {

    Calendar c = Calendar.getInstance();

    Date now = null;

    try {

    c.set(Calendar.DATE, 1);

    now = shortSdf.parse(shortSdf.format(c.getTime()));

    } catch (Exception e) {

    e.printStackTrace();

    }

    return now;

    }

    /**

    * 当前月的结束时间,即2012-01-31 23:59:59

    *

    * @return

    */

    public   Date getCurrentMonthEndTime() {

    Calendar c = Calendar.getInstance();

    Date now = null;

    try {

    c.set(Calendar.DATE, 1);

    c.add(Calendar.MONTH, 1);

    c.add(Calendar.DATE, -1);

    now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");

    } catch (Exception e) {

    e.printStackTrace();

    }

    return now;

    }

    /**

    * 当前年的开始时间,即2012-01-01 00:00:00

    *

    * @return

    */

    public   Date getCurrentYearStartTime() {

    Calendar c = Calendar.getInstance();

    Date now = null;

    try {

    c.set(Calendar.MONTH, 0);

    c.set(Calendar.DATE, 1);

    now = shortSdf.parse(shortSdf.format(c.getTime()));

    } catch (Exception e) {

    e.printStackTrace();

    }

    return now;

    }

    /**

    * 当前年的结束时间,即2012-12-31 23:59:59

    *

    * @return

    */

    public   Date getCurrentYearEndTime() {

    Calendar c = Calendar.getInstance();

    Date now = null;

    try {

    c.set(Calendar.MONTH, 11);

    c.set(Calendar.DATE, 31);

    now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");

    } catch (Exception e) {

    e.printStackTrace();

    }

    return now;

    }

    /**

    * 当前季度的开始时间,即2012-01-1 00:00:00

    *

    * @return

    */

    public   Date getCurrentQuarterStartTime() {

    Calendar c = Calendar.getInstance();

    int currentMonth = c.get(Calendar.MONTH) + 1;

    Date now = null;

    try {

    if (currentMonth >= 1 && currentMonth <= 3)

    c.set(Calendar.MONTH, 0);

    else if (currentMonth >= 4 && currentMonth <= 6)

    c.set(Calendar.MONTH, 3);

    else if (currentMonth >= 7 && currentMonth <= 9)

    c.set(Calendar.MONTH, 4);

    else if (currentMonth >= 10 && currentMonth <= 12)

    c.set(Calendar.MONTH, 9);

    c.set(Calendar.DATE, 1);

    now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");

    } catch (Exception e) {

    e.printStackTrace();

    }

    return now;

    }

    /**

    * 当前季度的结束时间,即2012-03-31 23:59:59

    *

    * @return

    */

    public   Date getCurrentQuarterEndTime() {

    Calendar c = Calendar.getInstance();

    int currentMonth = c.get(Calendar.MONTH) + 1;

    Date now = null;

    try {

    if (currentMonth >= 1 && currentMonth <= 3) {

    c.set(Calendar.MONTH, 2);

    c.set(Calendar.DATE, 31);

    } else if (currentMonth >= 4 && currentMonth <= 6) {

    c.set(Calendar.MONTH, 5);

    c.set(Calendar.DATE, 30);

    } else if (currentMonth >= 7 && currentMonth <= 9) {

    c.set(Calendar.MONTH,

    55cb493f430323b6c99c9e95f7130ab2.gif

    c.set(Calendar.DATE, 30);

    } else if (currentMonth >= 10 && currentMonth <= 12) {

    c.set(Calendar.MONTH, 11);

    c.set(Calendar.DATE, 31);

    }

    now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");

    } catch (Exception e) {

    e.printStackTrace();

    }

    return now;

    }

    /**

    * 获取前/后半年的开始时间

    * @return

    */

    public   Date getHalfYearStartTime(){

    Calendar c = Calendar.getInstance();

    int currentMonth = c.get(Calendar.MONTH) + 1;

    Date now = null;

    try {

    if (currentMonth >= 1 && currentMonth <= 6){

    c.set(Calendar.MONTH, 0);

    }else if (currentMonth >= 7 && currentMonth <= 12){

    c.set(Calendar.MONTH, 6);

    }

    c.set(Calendar.DATE, 1);

    now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");

    } catch (Exception e) {

    e.printStackTrace();

    }

    return now;

    }

    /**

    * 获取前/后半年的结束时间

    * @return

    */

    public   Date getHalfYearEndTime(){

    Calendar c = Calendar.getInstance();

    int currentMonth = c.get(Calendar.MONTH) + 1;

    Date now = null;

    try {

    if (currentMonth >= 1 && currentMonth <= 6){

    c.set(Calendar.MONTH, 5);

    c.set(Calendar.DATE, 30);

    }else if (currentMonth >= 7 && currentMonth <= 12){

    c.set(Calendar.MONTH, 11);

    c.set(Calendar.DATE, 31);

    }

    now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");

    } catch (Exception e) {

    e.printStackTrace();

    }

    return now;

    }

    }

    /**

    * 获取前一天日期 及星期

    */

    public class RemindDateUtils {

    private void initDate(){

    String[] weekDays = {"周日","周一","周二","周三","周四","周五","周六"};

    Calendar cal = Calendar.getInstance();

    cal.add(Calendar.Date,-1);

    int i = cal.get(Calendar.DAY_OF_WEEK)-1;

    if(i<0){

    i=0;

    }

    String yesterday = new SimpleDateFormate("yyyy年MM月dd日").format(cal.getTime());

    String w = weekDays[i];

    }

    }

    PS:SimpleDateFormat是线程不安全的

    展开全文
  • JAVA获取当前年的第一天和最后一天

    千次阅读 2019-04-07 23:20:47
    import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; * @author li_yueling * @version 1.0 2011-03-25 * */ public class DateTest { /** ...

     import java.text.SimpleDateFormat;   
    import java.util.Calendar;   
    import java.util.Date;   
      
     
     * @author li_yueling  
     * @version 1.0 2011-03-25  
     *  
    */  
    public class DateTest {   
        /**  
         * 默认日期格式  
         */  
        public static String DEFAULT_FORMAT = "yyyy-MM-dd";   
      
        /**  
         * 测试主方法  
         * @param args  
         */  
        public static void main(String[] args) {   
     
            for(int i = 1951;i < 1960;i++){   
                System.out.println(formatDate(getCurrYearFirst(i)));   
                System.out.println(formatDate(getCurrYearLast(i)));   
            }   
      
        }   
           
        /**  
         * 格式化日期  
         * @param date 日期对象  
         * @return String 日期字符串  
         */  
        public static String formatDate(Date date){   
            SimpleDateFormat f = new SimpleDateFormat(DEFAULT_FORMAT);   
            String sDate = f.format(date);   
            return sDate;   
        }   
           
        /**  
         * 获取某年第一天日期  
         * @param year 年份  
         * @return Date  
         */  
        public static Date getCurrYearFirst(int year){   
            Calendar calendar = Calendar.getInstance();   
            calendar.clear();   
            calendar.set(Calendar.YEAR, year);   
            Date currYearFirst = calendar.getTime();   
            return currYearFirst;   
        }   
           
        /**  
         * 获取某年最后一天日期  
         * @param year 年份  
         * @return Date  
         */  
        public static Date getCurrYearLast(int year){   
            Calendar calendar = Calendar.getInstance();   
            calendar.clear();   
            calendar.set(Calendar.YEAR, year);   
            calendar.roll(Calendar.DAY_OF_YEAR, -1);   
            Date currYearLast = calendar.getTime();   
               
            return currYearLast;   
        }   
      
    }  
     

    展开全文
  • Java 获取当前年 、当前月

    万次阅读 2018-10-17 16:45:34
    1、String year = String.valueOf(cal.get(Calendar.YEAR));//当前年 2、int month = cal.get(Calendar.MONTH )+1;
  • Java 获取当前年的第一天和最后一天

    千次阅读 2019-08-14 18:09:31
    import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * @author lzw * @Date 2019年8月8日 */ public class abc { public static void main(String[]...
  • 日历如何获取当前年的每一个月份的第一天和最后一天 实现2月的平年瑞年最后天数 首先先了解一下日历的两个方法 getLeastMaximum getActualMaximum   import java.text.SimpleDateFormat; import java.util...
  • new GregorianCalendar().isLeapYear(year) ? 366 : 365;
  • }/*** 获取日期年份 *@paramdate 日期 *@return */ public staticString getYear(Date date) {return format(date).substring(0, 4); }/*** 功能描述:返回月 * *@paramdate * Date 日期 *@return返回月份*/ public...
  • 实现2月的平年瑞年最后天数首先先了解一下日历的两个方法getLeastMaximumgetActualMaximumimport java.text.SimpleDateFormat;import java.util.Calendar;public class DateUtil {public static String ...
  • Java获取当前天,本年第一天,本年最后一天 /** * 日期工具类 */ public class DateUtils { /** * 获取今天 * @return String * */ public static String getToday(){ return new SimpleDateFormat("yyyy-MM...
  • 主要为大家详细介绍了Java获取当前时间年月日的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
  • import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;/***User:冯玉敏* Date: 11-8-9* Time: 下午1:23*/public class DateUtil {private static SimpleDateForm...
  • import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class TestTime { /** * 英文简写(默认)如:2010-12-01 */ public static String FORMAT_SHORT = "yyyy-MM
  • 最近在做统计相关的功能的时候涉及到了获取当天的开始和结束的时间戳、当月和当年的开始结束时间戳,特此记录,以作备忘。相关代码package com.lingyejun.authenticator;import java.time.Instant;import java.time....
  • Java获取当前系统的年份

    万次阅读 多人点赞 2019-04-14 00:09:03
    方法1, public static String getSysYear() { Calendar date = Calendar.getInstance(); String year = String.valueOf...喜欢本文的朋友们,欢迎关注微信公众号“Java面试达人”,收看更多精彩内容  
  • 方便自己以后查看,特记录下来。...import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import static com.fasterxml.jackson.databind.util.ISO8601Utils.fo...
  • importjava.util.Calendar;publicclassMain{publicstaticvoidmain(String[]args){Calendarcal=Calendar.getInstance();intday=cal.get(Calendar.DATE);intmonth=cal.get(Calendar.MONTH)+1;intyear=...
  • Java获取当前年份的整数值

    千次阅读 2021-03-08 05:29:59
    int year = Calendar.getInstance()....)使用Java 8的时间API(假设您很高兴在系统的默认时区获得Year::now ),您可以使用Year::now方法 :int year = Year.now().getValue();这个最简单的(使用日历,抱歉)是:int ...
  • 两种方法,通过Date类或者通过Calendar类。...import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.Locale;impo...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 3,644
精华内容 1,457
关键字:

java获取当前年

java 订阅
友情链接: CSharp.rar