Tag: 日期

将毫秒转换为年,月和日的最佳方法

我正在尝试将毫秒日期转换为数weeks和数days的years days 。 例如: 5 months, 2 weeks and 3 days或1 year and 1 day 。 我不想要: 7 days或4 weeks >这应该是1 week 1 month 。 我尝试了几种方法,但它总是变成7 days and 0 weeks 。 我的代码: int weeks = (int) Math.abs(timeInMillis / (24 * 60 * 60 * 1000 * 7)); int days = (int) timeInMillis / (24 * […]

在Java中获取文件属性

给定表示.exe文件的java.io.File ,如何发现文件属性 – 例如:创建的大小和日期,以及如何发现可执行属性中的数据 – 例如:文件版本?

如何正确计算2个日期之间的实际月数?

我遵循方法getDiffDateMap计算2个日期之间的差异,并返回分别代表毫秒,秒,分钟,小时,天,月和年的整数Map 。 public static Map getDiffDateMap(String dateA, String dateB) { Calendar cal = Calendar.getInstance(); Map out = new LinkedHashMap(); long timeInMillA = 0; long timeInMillB = 0; SimpleDateFormat dateFormat = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”); Date convertedDateA; Date convertedDateB; try { convertedDateA = dateFormat.parse(dateA); cal.setTime(convertedDateA); timeInMillA = cal.getTimeInMillis(); convertedDateB = dateFormat.parse(dateB); cal.setTime(convertedDateB); timeInMillB = cal.getTimeInMillis(); } catch […]

使用Jackson将使用generics的类序列化为JSON

我有一个表示调查问卷的对象结构,我需要序列化为JSON。 结构的一个类是OpenQuestion,这个类使用带有两个参数的generics。 当使用的类型之一是Date时,问题开始,日期序列化错误,如长。 class级代码: public class OpenQuestion extends AbstractQuestion implements Serializable { private valueType value; private validationType minValue; private validationType maxValue; … } 我看到如果哈希映射总是使用Date,如何在哈希映射中序列化日期,但在这种情况下,我使用带有String,Integer或Date的类。 有什么想法解决它吗? 谢谢

java.time.format.DateTimeParseException:无法在索引5处解析文本“2016-2-2”

我试图从import java.time.LocalDate;创建一个LocalDate实例import java.time.LocalDate; 我跟着这个 这是我的代码: LocalDate sd= LocalDate.parse(“2016-2-2”); 我遇到了错误: java.time.format.DateTimeParseException: Text ‘2016-2-2’ could not be parsed at index 5 at java.time.format.DateTimeFormatter.parseResolved0(Unknown Source) 在另一个尝试创建LocalDate的实例,我试过 LocalDate ed= new LocalDate(“2016-2-4”); 但它再次抱怨: The constructor LocalDate(String) is undefined

获取上个季度的结束日期

在给定日期,如何获得上一季度的结束日期? 我需要经营一份工作,考虑到这一点。 编辑:第一季度是1月,2月,3月; 第二个是4月,5月,6月,等等; 任何帮助表示赞赏。 谢谢

Java Date排序方法?

我有一个字符串数组,格式为ex:’2010-05-04 11:26:46 +0530’。 如何检查数组中的特定日期是>今天? 谢谢

java SimpleDateFormat模式与参数不同

SimpleDateFormat模式是“yyyyMM”,arg是yyyy-MM,但没有exception和错误的结果。 为什么? THX ~~ SimpleDateFormat format = new SimpleDateFormat(“yyyyMM”); System.out.println(format.format(format.parse(“2011-07”))); 结果是201105

我怎样才能在java日期对象中更改月份?

我有java日期对象。 如何在不改变年份的情况下更改月份。 例如 14年12月1日 我想把它改成 12/3/14和12/10/14 我基本上想在不改变年份的情况下将月份改变+/- x月。 可以吗?

如何在Android上解析ISO 8601字符串到Java日期

我正在Android上创建一个与服务器通信的应用程序。 该服务器给我一个ISO 8601日期字符串,如下所示: 2014-11-21 12:24:56.662061-02 然后我尝试使用Java的SimpleDateFormatter来解析我的String,如下所示: Locale l = new Locale(“pt”,”BR”); Date date = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss.SSSSSSZZ”,l).parse(str_date); Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(date.getTime()); 事实是,这个解决方案部分工作。 我能够正确地获得年,月,日和小时,但是当它达到小时,分钟和秒时,我总是错误地分钟。 如果我尝试使用我的示例字符串打印它,我会得到类似“12:35”而不是“12:24”的内容。 我尝试过不同的面具,使用Locale,而不是使用Locale,似乎没有什么对我有用。 我在这个链接上看到SimpleDateFormatter不能很好地支持ISO 8601,那个人使用javax.xml.bind.DatatypeConverter.parseDateTime()方法给出了解决方案,但Android SDK上没有DatatypeConverter。 那么……我能做些什么来正确解析这个字符串?