精华内容
下载资源
问答
  • How to display the date, month, and year of a particular month in for loop dynamically in Java?解决方案This demonstrates briefly some of the basics of the SimpleDateFormat and GregorianCalendar ...

    How to display the date, month, and year of a particular month in for loop dynamically in Java?

    解决方案

    This demonstrates briefly some of the basics of the SimpleDateFormat and GregorianCalendar classes in Java. It was the best I could do based on your question.

    import java.text.SimpleDateFormat;

    import java.util.GregorianCalendar;

    public class Main {

    public static void main(String[] args) {

    int year = 2012;

    int month = 4;

    /* The format string for how the dates will be printed. */

    SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");

    /* Create a calendar for the first of the month. */

    GregorianCalendar calendar = new GregorianCalendar(year, month, 1);

    /* Loop through the entire month, day by day. */

    while (calendar.get(GregorianCalendar.MONTH) == month) {

    String dateString = format.format(calendar.getTime());

    System.out.println(dateString);

    calendar.add(GregorianCalendar.DATE, 1);

    }

    }

    }

    展开全文
  • I was wondering how to do the same thing in newer Java 7 or Java 8. private boolean inCurrentMonth(Date givenDate) { Date today = new Date(); return givenDate.getMonth() == today.getMonth() && given...

    I need to check if a given date falls in the current month, and I wrote the following code, but the IDE reminded me that the getMonth() and getYear() methods are obsolete. I was wondering how to do the same thing in newer Java 7 or Java 8.

    private boolean inCurrentMonth(Date givenDate) {

    Date today = new Date();

    return givenDate.getMonth() == today.getMonth() && givenDate.getYear() == today.getYear();

    }

    解决方案

    Time Zone

    The other answers ignore the crucial issue of time zone. A new day dawns earlier in Paris than in Montréal. So at the same simultaneous moment, the dates are different, "tomorrow" in Paris while "yesterday" in Montréal.

    Joda-Time

    The java.util.Date and .Calendar classes bundled with Java are notoriously troublesome, confusing, and flawed. Avoid them.

    Instead use either Joda-Time library or the java.time package in Java 8 (inspired by Joda-Time).

    Here is example code in Joda-Time 2.5.

    DateTimeZone zone = DateTimeZone.forID( "America/Montreal" );

    DateTime dateTime = new DateTime( yourJUDate, zone ); // Convert java.util.Date to Joda-Time, and assign time zone to adjust.

    DateTime now = DateTime.now( zone );

    // Now see if the month and year match.

    if ( ( dateTime.getMonthOfYear() == now.getMonthOfYear() ) && ( dateTime.getYear() == now.getYear() ) ) {

    // You have a hit.

    }

    For a more general solution to see if a moment falls within any span of time (not just a month), search StackOverflow for "joda" and "interval" and "contain".

    展开全文
  • 根据订单创建时间获取当月订单总金额 List<String> dateList = new ArrayList<>();//存时间 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");//时间格式 Calendar calendar ...

    根据订单创建时间获取当月订单总金额

    需要显示月份数据

    		List<String> dateList = new ArrayList<>();//存时间
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");//时间格式
            Calendar calendar = Calendar.getInstance();
            for (int i = 1; i < 10; i++) {
                dateList.add(simpleDateFormat.format(calendar.getTime()));
                calendar.add(Calendar.MONTH, -1);
            }
            System.out.println(dateList.toString());
    

    显示数据

    StringTemplate 模板引擎 – 需要更深了解,自行面向网络编程

    StringTemplate不需要使用月份开始 和 结束 between and 去数据库取数据了

    以往:2020.01.01 00:00:00 <= date < 2020.02.01 00:00:00
    现在:使用2020.01即可

            //根据订单创建时间获取当月订单总金额
            QOrder qOrder = QOrder.order;
            //构造模版
            StringTemplate template = Expressions.stringTemplate("DATE_FORMAT({0},{1})", QOrder.createDate, "%Y-%m");
            List<Long> priceList = jpaQueryFactory.
                    select(qOrder.price.sum())
                    .from(qOrder).where(template.in(dateListNew))
                    .fetch();
    
    展开全文
  • 简单的一个小问题,在项目中遇到了,我... * 根据日期字符串判断当月周 * @param str * @return * @throws Exception */ public static int getWeek(String str) throws Exception{ SimpleDateF...

    简单的一个小问题,在项目中遇到了,我感觉很实用,便记录了下来。2014年5月15日。

     /**
    	  * 根据日期字符串判断当月第几周
    	  * @param str
    	  * @return
    	  * @throws Exception
    	  */
    	 public static int getWeek(String str) throws Exception{
    		 SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
    		 Date date =sdf.parse(str);
    		 Calendar calendar = Calendar.getInstance();
    		 calendar.setTime(date);
    		 //第几周
    	     int week = calendar.get(Calendar.WEEK_OF_MONTH);
    	     //第几天,从周日开始
    	     int day = calendar.get(Calendar.DAY_OF_WEEK);
    	     return week;
    	 }

    上面是我项目中遇到的,同时也记录一些时间格式的转换问题:

    1.Calendar 转化 String
    Calendar calendat = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String dateStr = sdf.format(calendar.getTime());
     
    2.String 转化Calendar
    String str="2012-5-27";
    SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
    Date date =sdf.parse(str);
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
     
    3.Date 转化String
    SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
    String dateStr=sdf.format(new Date());
     
    4.String 转化Date
    String str="2012-5-27";
    SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
    Date date= sdf.parse(str);
     
    5.Date 转化Calendar
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new java.util.Date());
     
    6.Calendar转化Date
    Calendar calendar = Calendar.getInstance();
    java.util.Date date =calendar.getTime();
     
    7.String 转成 Timestamp
    Timestamp ts = Timestamp.valueOf("2012-1-14 08:11:00");
     
    8.Date 转 TimeStamp
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String time = df.format(new Date());
    Timestamp ts = Timestamp.valueOf(time);


    展开全文
  • Java获取当月开始时间和当月结束时间
  • Java打印当月日历

    2021-04-03 22:36:46
    import java.time.DayOfWeek; import java.time.LocalDate; // 打印当月日历 public class CalendarTest { public static void main(String[] args) { ... // 获取每个数字表示 1-12 表示每个 int month =
  • java 当月最后一天

    2020-03-09 13:39:56
    public static Long getMonthLastDate(date){ try{ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); ...StringUtils.isEmpty(date)){ ...
  • java日期判断当月周,

    千次阅读 2019-04-12 14:00:05
    /** * 根据日期 判断当月周 * 从周日周一周二开始计算都可以 * @param str * @return * @throws Exception */ public static int getWeekNum(String str){ int weekOfMonth = 0; ...
  • Java生成当月日历

    2020-09-03 21:48:08
    import java.time.DayOfWeek; import java.time.LocalDate; public class Demo02 { public static void main(String[] args) { LocalDate date = LocalDate.now(); //当前的年月日 int month = date....
  • Java 输出当月日历

    2018-12-17 17:17:22
    import java.util.Calendar; public class CalendarTest3 { public static void main(String[] args) { // 输出当月日历 Calendar c = Calendar.getInstance(); // 设置日期为当月1号 c.set(Calendar.DATE...
  • java得到当月天数

    千次阅读 2015-02-26 16:26:02
    [java] view plaincopy /** * 取得当月天数 * */ public static int getCurrentMonthLastDay() { Calendar a = Calendar.getInstance(); a.set(Calendar.DATE, 1);//把日期设置为当月第一
  • JAVA获取当月第一天和最后一天以及当月的总天数二话不说直接上代码,拿走 二话不说直接上代码,拿走 // 获取当月第一天和最后一天 Calendar cale = null; cale = Calendar.getInstance(); SimpleDateFormat ...
  • java实现当月日历功能

    2020-05-12 15:25:40
    java核心技术》笔记——java实现日历功能 要点 确定今天的月份和日 确定本月的第一天 确定本月第一天是周几 第一天前面填充空格,间隔用空格或星号, 周日后换行 package com.company; import java.time....
  • java输出当月日历

    2015-01-30 15:48:05
    import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class CalendarTest3 { public static void main(String[] args) { System.out.printf("今天的日期是: %tF %
  • Java获取当月最后3天或

    千次阅读 2016-09-02 17:26:58
    import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; /** * Created by Administrator on 2016/8/25. */ public class Hello { public static void main(String args
  • Java之输出当月日历

    2021-05-19 14:50:53
    java类库中的LocalDate类 package com.company; import java.time.*; //实现输出当前月份的日历,并且标出今天的日期和星期 public class study { public static void main(String[] args) { //构造对象,表示...
  • 相信日期操作大家都会用到,有时需要获取指定日期当月和下个的第一天,这个怎么处理呢,话不多说上干货。/** * 获取指定日期当月的第一天 * @param dateStr * @param format * @return */ public static ...
  • java获取当月的工作日

    万次阅读 2014-11-15 22:39:20
    import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import com.taagoo.wenbo.common.util.DateUtils; public class Test { private st
  • Java获取当月的每一天

    2021-03-17 16:28:11
    Java获取当月的每一天 List<String> list = new ArrayList<String>(); Calendar aCalendar = Calendar.getInstance(Locale.CHINA); int year = aCalendar.get(Calendar.YEAR);//年份 int month = a...
  • java获取当月所有天

    千次阅读 2019-06-22 12:13:06
    * 获取当月所有天 * @return */ public static List getDayListOfMonth() { List list = new ArrayList(); Calendar aCalendar = Calendar.getInstance(Locale.CHINA); int year = aCalendar.get(Calendar....
  • import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; import java.util.Scanner; public class Project { public...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 12,862
精华内容 5,144
关键字:

java当月是几月

java 订阅