simpledateformat

Improper result of SimpleDateFormat

本秂侑毒 提交于 2019-12-18 09:42:17
问题 I want to convert Tue Jun 01 00:00:00 IST 112 into 01-JUN-2012 I used Calendar calendar = new GregorianCalendar(year, 6, Calendar.DAY_OF_MONTH); Date maxDate=new Date(); maxDate=calendar.getTime(); calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH)); SimpleDateFormat s=new SimpleDateFormat("dd-mmm-yyyy"); s.format(maxDate); But I get 30-000-0112 回答1: Use CAPITAL M for month, SimpleDateFormat s=new SimpleDateFormat("dd-MMM-yyyy"); Also you are setting date

Change date format

折月煮酒 提交于 2019-12-18 09:39:31
问题 I'm having trouble changing the date format to dd/MMM/yyyy . Here is my current implementation: final String OLD_FORMAT = "yyyy-MM-dd"; final String NEW_FORMAT = "yyyy-MMM-dd"; //Start Date String str4=label.getText(); java.util.Date toDate = null; //System.out.println(str4); //End Date String str5=lblNewLabel_3.getText(); java.util.Date newDateString = null; SimpleDateFormat format = new SimpleDateFormat(OLD_FORMAT); try { toDate=format.parse(str4); } catch (ParseException e1) { // TODO Auto

converting date to string and back to date in java

倾然丶 夕夏残阳落幕 提交于 2019-12-18 09:36:19
问题 In my program I am trying to convert the date to string to specified format and then back to date. I need the date to be in dd-MMM-yy format. So, I am converting the date to dd-MMM-yy format using SimpleDateFormat like, SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yy"); String bookingDate = sdf.format(myDate); The result is what I expected, that is 23-May-12 but when I convert the string back to date using parse(), it is changing to Wed May 23 13:16:14 IST 2012. Is there any way to

Why does a new SimpleDateFormat object contain calendar with the wrong year?

时光总嘲笑我的痴心妄想 提交于 2019-12-18 07:46:52
问题 I came upon a strange behavior that has left me curious and without a satisfactory explanation as yet. For simplicity, I've reduced the symptoms I've noticed to the following code: import java.text.SimpleDateFormat; import java.util.GregorianCalendar; public class CalendarTest { public static void main(String[] args) { System.out.println(new SimpleDateFormat().getCalendar()); System.out.println(new GregorianCalendar()); } } When I run this code, I get something very similar to the following

java.text.ParseException: Unparseable date: yyyy-MM-dd HH:mm:ss.SSSSSS

余生颓废 提交于 2019-12-18 05:59:14
问题 I am getting ParseException for the following code String dateStr = "2011-12-22 10:56:24.389362"; String formatStr = "yyyy-MM-dd HH:mm:ss.SSSSSS"; Date testDate = null; SimpleDateFormat sdf= new SimpleDateFormat(formatStr); sdf.setLenient(false); testDate = sdf.parse(dateStr); System.out.println("CHECK DATE " + sdf.format(testDate)); Exception in thread "main" java.text.ParseException: Unparseable date: "2011-12-22 10:56:24.389362" at java.text.DateFormat.parse(DateFormat.java:337) If I

SimpleDateFormat Week Calculations

天涯浪子 提交于 2019-12-18 05:55:26
问题 I'm getting some puzzling results with SimpleDateFormat and am hoping that someone can shed some light on the issue. The output: Time = Mon Dec 27 00:00:00 PST 2010 2010-01 <--- THIS IS WHAT I DON'T UNDERSTAND Start of week = Sun Dec 26 00:00:00 PST 2010 2010-01 End of Week = Sat Jan 01 23:59:59 PST 2011 2011-01 Should I be treating the last "week" of the year that extends to the next year as a special case? Or is this the correct way to interpret this? Obviously when attempting to organize

SimpleDateFormat Week Calculations

只愿长相守 提交于 2019-12-18 05:55:10
问题 I'm getting some puzzling results with SimpleDateFormat and am hoping that someone can shed some light on the issue. The output: Time = Mon Dec 27 00:00:00 PST 2010 2010-01 <--- THIS IS WHAT I DON'T UNDERSTAND Start of week = Sun Dec 26 00:00:00 PST 2010 2010-01 End of Week = Sat Jan 01 23:59:59 PST 2011 2011-01 Should I be treating the last "week" of the year that extends to the next year as a special case? Or is this the correct way to interpret this? Obviously when attempting to organize

Inconsistent date parsing using SimpleDateFormat

此生再无相见时 提交于 2019-12-18 04:43:11
问题 I'm really scratching my head on this one. I've been using SimpleDateFormat s with no troubles for a while, but now, using a SimpleDateFormat to parse dates is (only sometimes) just plain wrong. Specifically: SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date date = sdf.parse("2009-08-19 12:00:00"); System.out.print(date.toString()); prints the string Wed Aug 19 00:00:00 EDT 2009 . What the heck? - it doesn't even parse into the wrong date all the time! Update: That

Strange problem with timezone, calendar and SimpleDateFormat

[亡魂溺海] 提交于 2019-12-18 04:14:10
问题 Let's consider the following code: SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy", Locale.US); long start = sdf.parse("10:30:00 30/09/2009").getTime(); long end = sdf.parse("10:30:00 30/10/2009").getTime(); Calendar c = Calendar.getInstance(Locale.US); c.setTimeInMillis(start); System.out.println("Start = " + c.getTime()); c.setTimeInMillis(end); System.out.println(" End = " + c.getTime()); When running this code snippet, I have the following output: Start = Wed Sep 30 10

springboot定时任务

感情迁移 提交于 2019-12-18 04:07:13
今天用springboot写到一个需求:每周定时发送任务,但是要避开法定节假日。   网上找了些博客看,主要参考了https://www.cnblogs.com/lic309/p/4089633.html,整理补充了一下,完成需求。   (另,如果想要实时动态更新定时任务,可以参考http://blog.csdn.net/liuchuanhong1/article/details/60873295)   为了避开节假日,我写触发器,试了下用quartz的Calendar和HolidayCalendar,代码略复杂。放弃。 import org.quartz.Calendar;import org.quartz.impl.calendar.HolidayCalendar;   后来就弄了个数据库,存放需避开的日期,维护的时候只需更改数据库里的日期即可。(这个按照实际需求来,反复查找数据库,会消耗资源)   类如下。 import lombok.Data; import javax.persistence.Entity; import javax.persistence.Id; /** * Created by 孙义朗 on 2017/11/9 0009. */ @Data @Entity public class Holiday { @Id private String