java.util.date

Good Reason to use java.util.Date in an API

99封情书 提交于 2019-12-23 07:15:18
问题 Are there any specific reasons to use a Date class in an API (for example, in an employee birth date field) rather that a long or Long. There is some discussion of this in: java-date-vs-calendar, but I would like to know specifically if there is any justification for using Dates, when a long (or Long) seems so much simpler. Of course I would use TimeZone and SimpleDateFormatter to parse and display dates in a GUI, and maybe Calendar to perform manipulations, but I am concerned only about the

How to convert java.util.Date to Java8 java.time.YearMonth

℡╲_俬逩灬. 提交于 2019-12-22 03:36:10
问题 How can I best convert a java.util.Date to a Java 8 java.time.YearMonth ? Unfortunately the following throws a DateTimeException : YearMonth yearMonth = YearMonth.from(date.toInstant()); results in: java.time.DateTimeException: Unable to obtain YearMonth from TemporalAccessor: 2015-01-08T14:28:39.183Z of type java.time.Instant at java.time.YearMonth.from(YearMonth.java:264) ... I need this functionality since I want to store YearMonth values in a database using JPA. Currently JPA does not

How to convert java.util.Date to Java8 java.time.YearMonth

醉酒当歌 提交于 2019-12-22 03:36:06
问题 How can I best convert a java.util.Date to a Java 8 java.time.YearMonth ? Unfortunately the following throws a DateTimeException : YearMonth yearMonth = YearMonth.from(date.toInstant()); results in: java.time.DateTimeException: Unable to obtain YearMonth from TemporalAccessor: 2015-01-08T14:28:39.183Z of type java.time.Instant at java.time.YearMonth.from(YearMonth.java:264) ... I need this functionality since I want to store YearMonth values in a database using JPA. Currently JPA does not

Convert unix timestamp between different timezones and different DST in Java

半腔热情 提交于 2019-12-20 04:13:08
问题 PROBLEM SOLVED External device was computing and sending non-standard 2 hours shifted timestamp, which hugely confused me and started this thread. TIMESTAMP BY ITSELF IS NOT AFFECTED BY TIMEZONES , timezones apply only when converting in/from human readable forms. I have timestamp (seconds from unix epoch) in UTC timezone - no DST (daylight saving time). I want timestamp (seconds from unix epoch) in "Europe/Prague" timezone, that uses DST. I used to think that unix timestamp is unbound by

java.util.date bug?

会有一股神秘感。 提交于 2019-12-20 02:50:56
问题 Is there a bug is java.util.Date? While doing some testing, I was setting the milliseconds to 2147483647 which should give me a date of 2038-01-19 03:14:07 but it is returning 1970-01-25 20:31:23. Also tried it with 4294967295 and get the wrong answer of 1970-02-19 17:02:47. Does anyone know of a work around import java.util.*; import java.io.*; /* centos 6.3 x64 java version "1.7.0_13" Java(TM) SE Runtime Environment (build 1.7.0_13-b20) Java HotSpot(TM) 64-Bit Server VM (build 23.7-b01,

getting java.lang.IllegalArgumentException: Illegal pattern character 'o'? while parsing java.text.SimpleDateFormat

∥☆過路亽.° 提交于 2019-12-20 01:57:10
问题 I wanted to convert from string to java.util.Date. for the same purpose I used following code, String timeStamp = "Mon Feb 14 18:15:39 IST 2011"; DateFormat formatter = new SimpleDateFormat("dow mon dd hh:mm:ss zzz yyyy"); Date ts = (Date)formatter.parse(timeStamp); The format given to SimpleDateFormat() is format of java.util.Date. When you convert util's Date to string it comes in this format('dow mon dd hh:mm:ss zzz yyyy'). But when I execute code, It gives me Exception. I Don't know what

String -> java.util.Date -> java.sql.Date (with time stamp)

强颜欢笑 提交于 2019-12-17 19:36:34
问题 Here is my problem: I have a user input a date like: 2012-12-24 (string) I concatenate a time to that string, and convert to java.util.Date My code looks like: String tempstartdate = startdte; //startdte is the string value from a txtfield tempstartdate += " 00:01:00"; String tempenddate = startdte; tempenddate += " 23:59:59"; SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); java.util.Date validstartdte = null; java.util.Date validenddte = null; validstartdte = df.parse

Value '0000-00-00' can not be represented as java.sql.Date

半腔热情 提交于 2019-12-17 15:51:59
问题 I'm working on some project which needs to extract data from DB and I use Spring MVC to build model from DB for selecting data. Here is the problem with my JSP page: <form action="result" method="get" > <table> <thead> <tr> <th>Date to select:</th> <th>Name to select:</th> <th>Type to select:</th> </tr> </thead> <tbody> <tr> <td><form:select path="listOfDates"> <form:option value="NONE"> --SELECT--</form:option> <form:options items="${listOfDates}"></form:options> </form:select> </td> <td>

Calculating the difference between two Java date instances

谁说我不能喝 提交于 2019-12-17 04:07:41
问题 I'm using Java's java.util.Date class in Scala and want to compare a Date object and the current time. I know I can calculate the delta by using getTime(): (new java.util.Date()).getTime() - oldDate.getTime() However, this just leaves me with a long representing milliseconds. Is there any simpler, nicer way to get a time delta? 回答1: The JDK Date API is horribly broken unfortunately. I recommend using Joda Time library. Joda Time has a concept of time Interval: Interval interval = new Interval

How to convert a long to a Date with “dd/MM/yyyy” format [duplicate]

最后都变了- 提交于 2019-12-14 03:29:47
问题 This question already has answers here : Converting long (whose decimal representation represents a yyyyMMdd.. date) to a different date format (2 answers) Closed last year . I have a variable of type Long. Long longDate = 20180201110400 It represents this: 2018/02/01 11:04:00 I want to convert the format and variable type like below: Format should be "dd/MM/yyyy" and type should be Date . How can I do that? 回答1: You can covert the long to a Date object first then you can further convert it