I save a Date and this is ever 1698-19-20 and not 2012-4-12. Here one sees the error?
Date saved as Integer.
calculated with the calculator:
1334262386066 millisecond
42.309182713914 year + 1970 Year = ~2012
Code:
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
calendarEntity.setDate(date);
sqlite query:
SELECT strftime('%Y-%m-%d',DATE) FROM CALENDAR_ENTITY
Table:
CREATE TABLE 'CALENDAR_ENTITY' ('_id' INTEGER PRIMARY KEY ,'TITLE' TEXT,'NOTICE' TEXT,'DATE' INTEGER,'BABY_ID' INTEGER NOT NULL )
If you can use this return long like this I think your problem will solved itself.
Date date = new Date(1334262386066l);
This will return what you expected. And this is what it prints Thu Apr 12 23:26:26 EEST 2012
The problem is that strftime function expects the date as text in one of these formats:
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
HH:MM
HH:MM:SS
HH:MM:SS.SSS
now
DDDD.DDDD
You are storing the date as integer which strftime function can't handle.
More details about SQLite datetime functions here (or here, page 80).
Related
I've got Timestamp format in DB and my setEventDate method needs Date format.
So in my DAO class there is something like this :
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
Timestamp dbDate = rows.getTimestamp("event_date");
String dbDateToString = new SimpleDateFormat("HH:mm").format(dbDate);
Date dbDateToDate = sdf.parse(dbDateToString);
e.setEventDate(dbDateToDate);
System.out.println(dbDateToString);
System.out.println(dbDateToDate);
I'm getting Timestamp from DB, format it to String and in next step I'm parsing it to Date. I know that it sounds weird. The result is:
String - 17:08
Date - Thu Jan 01 17:08:00 CET 1970
I don't get it :/ I need that "HH:mm" format.
You are taking a Timestamp from your DB, you need to change it a Date first
Timestamp dbDate = rows.getTimestamp("event_date");
Date d = new Date(dbDate.getTime();
now you can do your SimpleDateFormat formatting stuff
Timestamp dbDate = rows.getTimestamp("event_date");
Date d = new Date(dbDate.getTime());
String dbDateOnlyHourAndMinutes = new SimpleDateFormat("HH:mm").format(dbDate);
there you have your Hour and minutes only "Date".
You say you want to parse date but in your date format you are passing only hours (HH) and minutes (mm) format.
Check how to create your desired date format from :
What are the date formats available in SimpleDateFormat class?
https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
example of a date format is: dd/MM/yyyy.
In case i did not understood correctly please explain more.
Here is an example code to convert the timestamp to your format.
ASSUMPTION: desired date format is MM:yyyy:
Timestamp stamp = new Timestamp(System.currentTimeMillis());//your timestamp goes here
Date date = new Date(stamp.getTime());
System.out.println(date);//result = Tue Mar 27 18:50:09 EEST 2018
SimpleDateFormat sdf = new SimpleDateFormat("MM:yyyy");
System.out.println(sdf.format(date)); //result = 03:2018
I am using postgres 9.4(Date Column) as my database and would like to know if it is possible to insert a formatted date into the database using JDBC preparedstatement in the following format (I been searching around and couldn't find anything): Aug-21-2015. This is my code
Calendar calendar = Calendar.getInstance();
java.util.Date today = calendar.getTime();
SimpleDateFormat formatter= new SimpleDateFormat("MM-dd-yyyy");
java.util.Date mydate= formatter.parse(today.toString());
preparedStatement.setDate(2,new java.sql.Date(mydate.getTime()));
needless to say that does not work and I get this error
Unparseable date: "Fri Aug 21 01:17:59 EDT 2015"
again I am trying to get this into Aug-21-2015 , I can successfully execute the code if I only insert like this
Calendar calendar = Calendar.getInstance();
java.util.Date mydate = calendar.getTime();
preparedStatement.setDate(2,new java.sql.Date(mydate.getTime()));
but it is inserted as 8-21-2015, I was thinking of maybe just saving it as 8-21-2015 format and then just parsing all of them but would prefer the 1st option.
Your date pattern is wrong: it must be:
SimpleDateFormat formatter= new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
For mor information see the javadoc of SimpleDateFormat
A value in a date column itself is not formatted, it is just a date, if you want to format the date when you're reading it back out of the database, just use one of the date formatting functions.
E.g.
select to_char(your_date_column, 'MON-DD-YYYY') from yourtable
Don't know why you're converting to string and back, but today.toString() is NOT generating a text string that matches MM-dd-yyyy, so formatter.parse(today.toString()) will fail.
If the intent was to remove the time portion, in order to get "today", you can:
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
cal.clear();
cal.set(year, month, day);
java.sql.Date today = new java.sql.Date(cal.getTimeInMillis());
Or, with Java 8:
java.sql.Date today = java.sql.Date.valueOf(LocalDate.now());
Date Format
And for your desire the insert a "formatted date", then you are confused. A date column in the database doesn't have a format, it simply stores the date value.
When you query the date column, you get the date value, which can be retrieved in Java using the ResultSet.getDate() method.
When you query the date in a tool, the tool will convert the date to a text value for display. Some tools will allow you to control the date format.
If you otherwise want to control the date format, you can convert the date to a text column in the query, using DBMS specific formatting functions.
I am converting java Date object to epoch using code :
String str = "" + date;
SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss.SSS");
Date formateDate = df.parse(str);
long epoch = formateDate.getTime();
return epoch;
If I test this with value 2013-04-26 08:34:55.705 then it gives Long as 1359189295705 which is actually Sat, 26 Jan 2013 08:34:55 GMT but its Friday today why does it say that its Saturday on 26th January 2013.
It's using January not April. January 26th was indeed a Saturday. It's given you the wrong month because your date format is wrong:
yyyy-mm-dd HH:mm:ss.SSS
You're using mm for the month, when you should be using MM. The date format should be:
yyyy-MM-dd HH:mm:ss.SSS
Your format didn't specify the month anywhere, so presumably it just defaulted to January.
The format for month is wrong, should be yyyy-MM-dd, so it's defaulting to the first month for you.
Iam saving date in below format EEE MMM dd HH:mm:ss z yyyy, but it is saving in this format 2013-05-03 00:38:20.0. But when i print same date on console before saving to database, it is as expected INFO: Fri May 03 00:38:20 IST 2013
Please can anyone explain why it is saving as above mentioned.
Below is the code:
Date now = new Date();
String datetimeStr = now.toString();
SimpleDateFormat format = new SimpleDateFormat(
"EEE MMM dd HH:mm:ss z yyyy");
Date parseDate = format.parse(datetimeStr);
Datebase column
date_order_created date
I hope you're not actually saving your dates as text in the database in the first place. Assuming you're not (and I don't think you are), you're not really "saving date in below format" at all... you're just saving the date.
The value of a date doesn't have a format - June 19th 1976 is the same date whether I represent it that way, or as 1976-06-19 or 19/06/1976 or 06/19/1976 (assuming I know how to interpret any of those formats). That's what you're seeing here - you're saving the date, and when you look at it "in the database" (e.g. via some SQL console) you're seeing one representation... but when you fetch it from the database and print it from Java, you're seeing the results of calling Date.toString(). That doesn't change the value at all.
If you want to format a Date value in one particular way, use SimpleDateFormat - but be very aware that a Date object itself has no format (or time zone, or calendar) and your database probably doesn't either. (The exact details of what goes in the database will depend on the database and the column type.)
It's very important to distinguish between a value and string representations of that value.
For example, if I write:
int x = 0xff;
int y = 255;
System.out.println(x == y);
then that prints "true" - because both x and y have the same value. The fact that in one case I used a hexadecimal representation doesn't affect the value as a number at all.
From here
The following code formats a date and time according to the pattern String passed to the SimpleDateFormat constructor. The String returned by the format method contains the formatted date and time that are to be displayed.
Date today;
String output;
SimpleDateFormat formatter;
formatter = new SimpleDateFormat(pattern, currentLocale);
today = new Date();
output = formatter.format(today);
System.out.println(pattern + " " + output);
The following table shows the output generated by the previous code ex
Pattern Output
EEE, MMM d, ''yy Tue, Jun 30, '09
When you save a Date objet in a database, this is generally not its String representation which is saved. This depends of the field type used in the database.
And :
Fri May 03 00:38:20 IST 2013
looks like a date.toString()
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("PST"));
cal.setTime(new Date());
System.out.println("cal:"+cal);
System.out.println("cal.getime:"+cal.getTime());
output are:
cal:java.util.GregorianCalendar[time=1325177592164,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="PST",offset=-28800000,dstSavings=3600000,useDaylight=true,transitions=185,lastRule=java.util.SimpleTimeZone[id=PST,offset=-28800000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2011,MONTH=11,WEEK_OF_YEAR=53,WEEK_OF_MONTH=5,DAY_OF_MONTH=29,DAY_OF_YEAR=363,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=5,AM_PM=0,HOUR=8,HOUR_OF_DAY=8,MINUTE=53,SECOND=12,MILLISECOND=164,ZONE_OFFSET=-28800000,DST_OFFSET=0]
cal.getime:Thu Dec 29 22:23:12 IST 2011
Problems facing:
While print 'cal' object getting date and time as per time zone.
cal.getTime() not displaying date and time to as per timezone.
You should represent time in UTC (java.util.Date) and then display the time in the local timezone of the user. Use DateFormat and TimeZone to do that. Read this article for more details
Unfortunately Date object always display time in GMT. You need to use something like SimpleDateFormat to display time in formatted timezone.
assign user given time into string variable then create date formatter. then parse the given string using date formatter.
String dateAndTime = "30-12-2011 12:00:00 GMT+5.30"; dateFormate
formate = new SimpleDateFormate("dd-mm-yyyy hh:mm:ss z"); Date date =
formate.parse(dateAndTime );
Date object is Java doesn't store the TimeZone info.
If you do
new Date()
this is the current local time of the machine/jvm.
To print it with the default/local timezone info:
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z");
System.out.println(sdf.format(new Date()));