Tag: 日期

将1周添加到日期,首选哪种方式?

我正在审查工作中的一些代码,并发现代码处理如何处理当前时间1周的不一致,并想知道是否有任何理由为什么一个应该真正优先于另一个: 第一个是实用方法: public static Date addDaysToDate(final Date date, int noOfDays) { Date newDate = new Date(date.getTime()); GregorianCalendar calendar = new GregorianCalendar(); calendar.setTime(newDate); calendar.add(Calendar.DATE, noOfDays); newDate.setTime(calendar.getTime().getTime()); return newDate; } 第二个使用简单的毫秒算法: long theFuture = System.currentTimeMillis() + (86400 * 7 * 1000); Date nextWeek = new Date(theFuture); 第二种方法显然使用“幻数”来定义一周,但是这可以移动到一个常量MILLISECONDS_IN_ONE_WEEK = 86400 * 7 * 1000除此之外,是否有任何理由为什么这些方法中的一种应该优先于另一种? 基本上我想改变代码在整个过程中保持一致,但我不完全确定删除哪一个。 所以任何论据都是有用的。

字符串日期到xmlgregoriancalendar转换

我写过这个函数: public static XMLGregorianCalendar getXMLGregorianCalendar(String date) throws OmniException{ XMLGregorianCalendar xmlCalender=null; GregorianCalendar calender = new GregorianCalendar(); calender.setTime(Util.stringToJavaDate(date)); xmlCalender = DatatypeFactory.newInstance().newXMLGregorianCalendar(calender); return xmlCalender; } public static Date stringToJavaDate(String sDate) throws OmniException{ Date date=null; date = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”, Locale.ENGLISH).parse(sDate); return date; } 当我将日期作为”2014-01-07″传递时,我的输出日期为2014-01-06T18:30:00:000Z我哪里出错了? 如果我只想获得2014-01-06T18:30:00和2014-01-06T18:30:00Z该怎么办 任何帮助表示赞赏

在Java中将字符串转换为OffsetDateTime

我试图在OffsetDateTime转换一个字符串,但低于错误。 java.time.format.DateTimeParseException: Text ‘20150101’ could not be parsed: Unable to obtain OffsetDateTime from TemporalAccessor: {},ISO resolved to 2015-01-01 of type java.time.format.Parsed 代码: OffsetDateTime.parse(“20150101”, DateTimeFormatter.ofPattern(“yyyyMMdd”)); 预期输出: OffsetDateTime object with date 20150101. 我非常感谢您提供的任何帮助。 谢谢,

如何从Java中的日历对象构建日,月,年的列表?

我想为表单构建一个日期窗口小部件,其中包含月,日,年的选择列表。 因为列表根据月份和年份而不同,所以我不能将其编写为31天。 (例如,2月有28天不是30或31,有些年甚至29天)我如何使用日历或joda对象来构建这些列表。

使用Java中的MongoDB中的日期范围进行查询

我是使用MongoDB的新手。 我把它填满了收据,例如:一张收据如下: { “_id” : { “$oid” : “510fa057c6f818c2bfd0b279” } , “StoreName” : “Metro” , “StoreNumber” : 521 , “Items” : [ { “ItemName” : “Battery” , “ItemManufacturer” : “Duracell” , “ItemPrice” : 12 , “ItemQuantity” : 2 , “ItemTotalPrice” : 24 } , { “ItemName” : “TV CRT 25 inch” , “ItemManufacturer” : “Toshiba” […]

Java:跨越几个月的两个日期之间的差异

我有以下代码成功地让我得到两天之间的差异(以天,小时,分钟,秒为单位): SimpleDateFormat format = new SimpleDateFormat(“yyyy-MM-DD HH:mm:ss”); Date d1 = format.parse(startTime); Date d2 = format.parse(endTime); long diff = d2.getTime() – d1.getTime(); long diffSeconds = diff / 1000 % 60; long diffMinutes = diff / (60 * 1000) % 60; long diffHours = diff / (60 * 60 * 1000) % 24; long diffDays = diff […]

Java中的整数转换日期

我有一个int变量跟随。 如何将其转换为Date对象,反之亦然。 int inputDate=20121220;

Mongodb + Java驱动程序。 按日期范围搜索

这是我第一次使用Mongodb和java驱动程序。 我可以使用javascript和Date()对象通过命令行查询数据库,但是,我在使用驱动程序时遇到问题。 根据我的查询,任何人都可以看到问题是什么? 谢谢 Date current = new Date(); DBCollection coll = db.getCollection(“messages”); BasicDBObject query = new BasicDBObject(“created_on”, new BasicDBObject(“$gte”, new Date(current.getYear(), current.getMonth(), current.getDate())). append(“created_on”, new BasicDBObject(“$lt”, new Date(current.getYear(), current.getMonth() – 1, current.getDate())))); System.out.println(“Query: ” + query); DBCursor cursor = coll.find(query); 查询:{“created_on”:{“$ gte”:{“$ date”:“2012-12-06T05:00:00.000Z”},“created_on”:{“$ lt”:{“$ date”:“ 2012-11-06T05:00:00.000Z“}}}} PS如果不明显,我试图找到上个月内的所有记录。

向日历添加天数

我是这个网站的新手,我刚开始学习Java。 我试图给GregorianCalendar添加几天,但它不起作用。 这里……(忽略顶部块),它底部的添加日期令人讨厌。 /* * Author:Matt M * Date:8.12.13 * Discription: When the user inputs the deadline, and the difficulity of the project, * the program gives the date he should start working on it */ import java.util.*; public class DeadlinePlanner{ public static void main(String[] args) { //take information and restart questions if information is […]

If-Modified-Since日期格式

我现在正在写一个服务器,我想检查“If-Modified-Since:”标题。 由于有太多的日期方法,我应该考虑检查哪种方法,就像通常的方法(在浏览器中使用毫秒)…. Follwing是毫秒格式的。 Date date; //dateS is a substring of If-Modified-Since: header try{ long mills = Long.parseLong(dateS); } catch (NumberFormatException e) {e.printStackTrace(); } date = new Date(mills); 我还要查看“Wed,19 Oct 10:50:00 GMT”格式。 如何将该日期更改为毫秒? SimpleDateFormat dateFormat = new SimpleDateFormat( “EEE, dd MMM yyyy HH:mm:ss z”); // to check if-modified-since time is in the above format try { […]