How to convert timestamp 1470989229.000014 to date format mm-dd-yyyy in java?
I could do it for different timestamp formats but I could not parse this one.
The database used is MySQL so the timestamp probably would be in that format.
first question, what programming language did you used?
in case you're using PHP, you can try date("mm-dd-yyyy", (1470989229.000014)) but if you want the result like 08-12-2016 , you can use date("m-d-Y", (1470989229.000014))
In MySQL you can do this using FROM_UNIXTIME followed by DATE_FORMAT function.
SELECT DATE_FORMAT(FROM_UNIXTIME(1470989229.000014),'%m-%d-%Y')
And the output(mm-dd-yyyy) would look like below:
08-12-2016
Demonstrated Here
Related
I have certain object that has a date property. When i print the date value in Java i get a normal date like "11/08/2021". The thing is, when i try to check the date of my object (which is stored in mongoDB) in Robo 3T, i get a number like that : 1507037760657.
I searched and i found this kind of converter that allows to convert timeStamp format to date format. But that value : 1507037760657 is converted to some really weird date like 15/2/49726.
Is mongoDB using another timestamp format i don't know? How can i get a good date from this number ? 1507037760657
Thanks
I want to save the dates in my database in the dd/mm/yyyy format, because I want to import the table that contains them in a JTable (Java SE) and I want to display them in this format. Is it possible to directly save the date on my database in this format or I must do it in another way? My DB is write in SQL and I use MySQL.
Date are dates. It doesn't exists a format for dates.
What you can obtain is a string with a particular format from the date.
Note that the format probably is not dd/mm/yyyy but dd/MM/yyyy because mm is for minutes, not for months.
So basically you have two possibilities:
Save dates as Date and retrieve them as string with the requested format
Convert dates to strings and save them as formatted strings (VARCHAR for example)
To convert a Date to a String in MySql you can use the function DATE_FORMAT
If you like to convert them in java you can use a SimpleDateFormat
There is no possibility to save the date in the specified format but yes you can set the type of that field as String (in MySql varchar) and you can save whatever you want.
You can't do it,
or else you have to use VARCHAR or CHAR, but thats not recommended.
save the date in DATE datatype with format yyyy-mm-dd.
don't mess with it.
When you fetch the records, use DATE_FORMAT function to convert it into your format. (if you use MySQL)
like
SELECT DATE_FORMAT(CURDATE(), '%d-%m-%Y');
in your case
SELECT DATE_FORMAT(< your_date_field >, '%d-%m-%Y');
Use DATE_FORMAT function to change the format of a date in MySQL.
SELECT DATE_FORMAT(CURDATE(),'%d/%m/%Y')
SELECT DATE_FORMAT(column_name,'%d/%m/%Y') # FROM tablename
Refer to documentation for more details.
I am making a Application Using Java. to pick the date i am using JCalender.jar file , and saving it in MYSQL . their date format does not match MYSQL date format . how can i change it
First of all, this isn't really a Swing question but more of a JDBC question. As Jon Skeet already pointed out, you shouldn't be trying to embed a formatted date into a SQL statement. Instead, you should use PreparedStatement's setDate() method, passing in an instance of java.sql.Date. Using that method will insulate you from details like what date format the database expects (you won't know or care) which in turn means that your code won't break if it's run against a database expecting a different format from the one that would work for you today.
Is it possible to save date values in the database of this format dd/MM/yy in Grails? I know I can customized the format in the views but I need the values to be returned as json and also return the values of dates in json in that format. Any suggestion will be appreciated.
Try this on your presentation layer, don't save time in that format in database. use following code to format the time according to your need.
Date date = new Date( );
SimpleDateFormat simpleFormat = new SimpleDateFormat ("dd/MM/yy");
System.out.println("Date: " + simpleFormat .format(date));
But if you want to save the data in this format in databse, then remember it returns a string and you will have to save the date in String format in database. Which I wouldn't recommend because of many reason.
You should not store the date as a formatted string because you lose the ability to do many things with that field, such as sort it or compare it. Always use the database's native date format for storage. If you want to change the format there are many places to do it, including the presentation layer (as others have suggested) and the database query layer. Format the date in the query if you want to do minimal processing in Java/Javascript.
Agreed with others you should not save in the database in String format. To format a Date using Groovy you can use the String.format() method.
String.format('%tY-%<tm-%<td', new Date())
See the Groovy dates documentation for further examples.
I am using Facebook graph API to retrieve user wall information. In that, I am getting the post created time value as:
created_time: "2012-04-19T09:00:02+0000"
Can anyone suggest me how to convert this time to UTC or epoch value in Java?
The format of date you receive is ISO 8601.
As described in Converting ISO8601-compliant String to java.util.Date (using Joda-Time):
DateTimeFormatter parser = ISODateTimeFormat.dateTimeNoMillis();
String jtdate = "2012-04-19T09:00:02+0000";
System.out.println(parser.parseDateTime(jtdate));
You basically need to parse the ISO8601 date format. There are libraries out there that would do it for you or you can create your own parser (relatively simply), e.g.
http://www.java2s.com/Code/Java/Data-Type/ISO8601dateparsingutility.htm