java.util.date

java.util.Date和java.sql.Date的区别和相互转化

跟風遠走 提交于 2019-12-02 18:41:25
java.util.Date是在除了SQL语句的情况下面使用的。 java.sql.Date是针对SQL语句使用的,它只包含日期而没有时间部分 它 们都有getTime方法返回毫秒数,自然就可以直接构建。 java.util.Date 是 java.sql.Date 的父类,前者是常用的表示时间的类,我们通常格式化或者得到当前时间都是用他,后者之后在读写数据库的时候用他,因为PreparedStament的 setDate()的第2参数和ResultSet的getDate()方法的第2个参数都是java.sql.Date。 java.sql.Date转为java.util.Date java.sql.Date date=new java.sql.Date(); java.util.Date d=new java.util.Date (date.getTime()); java.util.Date转为java.sql.Date java.util.Date utilDate=new Date(); java.sql.Date sqlDate=new java.sql.Date(utilDate.getTime()); java.util.Date utilDate=new Date(); java.sql.Date sqlDate=new java.sql.Date(utilDate

Create a Java Date from a String and vise versa

北战南征 提交于 2019-12-02 17:47:23
问题 I have a date in Integer format (YYYYMMDD) . And a start_time as a String (HH:mm 24 hour system) . and a time_duration in hours as a double . int date = 20140214; String start_time = "14:30"; double duration = 50.30; I want to use these 3 values and create 2 Java Date Objects. One is start_date and one is end_date . They should be in the format YYYY-MM-DD HH:mm . And then after I get 2 data Strings like YYYY-MM-DD HH:mm . how can I obtain those previous variables. date , start_time , duration

Angular Js post date Bad Request in Java Spring mvc

社会主义新天地 提交于 2019-12-02 03:51:32
问题 In Java Spring MVC project, I post an object to a @RestController and the object I post has an date property. If I remove this property, the post works successfully. But with the date property, it returns a 400 bad request. In the dto the Date is java.util.Date Method in controller: @RequestMapping(value = "/users/createPetition", method = RequestMethod.POST) public @ResponseBody PetitionDTO addPetition(@RequestBody PetitionRequestDTO petitionDto, Model model) { ... PetitionRequestDTO public

Conversion of string with AM/PM date-time, from Oracle database

只谈情不闲聊 提交于 2019-12-01 22:16:13
问题 I have the timestamp in form of 03-AUG-12 08.15.00.000000000 PM -05:00 I am unable to get a String representation in form on yyyy-MM-dd HH:mm:ss . Here is my code : public static void convert() { String oldstring = "03-AUG-12 08.15.00.000000000 PM -05:00"; Date date = null; try { date = new SimpleDateFormat("dd-MMM-yy HH.mm.ss.S aa").parse(oldstring); } catch (ParseException e) { e.printStackTrace(); } String newstring = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date); System.out

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

痴心易碎 提交于 2019-12-01 18:47:50
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 exactly I needed to do to get rid of this problem. I am posting the part of StackTrace of exception. If

Why does java.util.Date represent Year as “year-1900”?

为君一笑 提交于 2019-12-01 06:30:33
In java.util.Date : * In all methods of class <code>Date</code> that accept or return * year, month, date, hours, minutes, and seconds values, the * following representations are used: * <ul> * <li>A year <i>y</i> is represented by the integer * <i>y</i><code>-1900</code>. Of course, in Java 1.1, the getYear() method and the like were deprecated in favor of java.util.Calendar , which still has this weird deprecation note: int getYear() Deprecated. As of JDK version 1.1, replaced by Calendar.get(Calendar.YEAR) - 1900. setYear(int year) Deprecated. As of JDK version 1.1, replaced by Calendar.set

Why does java.util.Date represent Year as “year-1900”?

女生的网名这么多〃 提交于 2019-12-01 06:06:36
问题 In java.util.Date : * In all methods of class <code>Date</code> that accept or return * year, month, date, hours, minutes, and seconds values, the * following representations are used: * <ul> * <li>A year <i>y</i> is represented by the integer * <i>y</i><code>-1900</code>. Of course, in Java 1.1, the getYear() method and the like were deprecated in favor of java.util.Calendar , which still has this weird deprecation note: int getYear() Deprecated. As of JDK version 1.1, replaced by Calendar

Are java.util.Date and java.util.Calendar deprecated?

♀尐吖头ヾ 提交于 2019-11-30 21:03:37
It seems that the new java.time API offers everything from java.util.Date and much more. Is there any reason to use java.util.Date when the newer java.time API is there since Java 8? Should java.util.Date and java.util.Calendar be avoided completely? Short answer: The new API java.time is way better than the old world with java.util.Date and java.util.Calendar . So yes, the new API should be preferred in new code. For a quick overview: Once I had written a comparison of features in table form for various date-time-libraries. There is almost no feature which java.time is missing but exists in

How would you represent date of birth in your java model?

怎甘沉沦 提交于 2019-11-30 09:39:42
And wait, don't rush to answer "java.util.Date", consider the following scenario. Person object having 2 fields: "birthday" and "nextMeeting" both java.util.Date. Now birthday stored in database as date type column (no time) for eg. 01-10-1979, and nextMeeting as datetime type for ex. 01-10-2010 20:00:00. You pull it from db, "birthday" will be auto set to midnight by JDBC. Now you need to send this object to other JVM using lets say RMI or whatever technology. On the other end JVM has timezone -1h from originating JVM. This is where problem starts. nextMeeting become 01-10-2010 19:00:00 which

How to remove milliseconds from Date Object format in Java

◇◆丶佛笑我妖孽 提交于 2019-11-30 07:10:32
问题 Since the java.util.Date object stores Date as 2014-01-24 17:33:47.214 , but I want the Date format as 2014-01-24 17:33:47 . I want to remove the milliseconds part. I checked a question related to my question... How to remove sub seconds part of Date object I've tried the given answer long time = date.getTime(); date.setTime((time / 1000) * 1000); but I've got my result Date format as 2014-01-24 17:33:47.0 . How can I remove that 0 from my Date format??? 回答1: Basic answer is, you can't. The