Inserting date in Mysql (codename one) - mysql

I want to insert a Date object in mysql database, which has a Date type in the database as well. I am having problems inserting the date .
I have tried this code, but it seems codename one have a problem with it:
dateString s;
s = date.getCurrentMonth() + "/" + date.getCurrentDay() + "/" + date.getCurrentYear();
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Date startDate = (Date) formatter.parse(s);
Please can you tell me how to do it ?

You don't need to format it. Just use this SQL Date Object instead of Date object from java.util package.
import java.sql.Date
// Creating a date object.
Date date = new Date();
In a database, make sure the data type of attribute 'date' is selected as "Date" also, not VarChar. Simply pass this sql package Date object into the database through query. :) It will save the date in a format.

Related

get DateTime from mysql database into android sqlite

I want to get data from mysql and store it in an android app. I could not find a Date or Time field in android. I have to save it to sqlite database and keep it for the next app update. In the next update I get the sqlite Date and check against the mysql database find out if there is a new post or not.
How can i get the data in the below loop?
for (int i = 0; i <count; i++)
{
JSONObject json_data = jArray.getJSONObject(i);
temp +=
"Name : " + json_data.getString("name")+ "\n"+
"Lastname : " + json_data.getString("lastname")+
date : " + //the DateTime "\n";
}
Although SQLite saves date as text or numeric, you can achieve features of mysql date/datetime in SQLite as well (indirectly).
Get datetime as per UTC timezone and save into SQLite db in milliseconds (long value) . SQLite takes numeric datatypes
Sample code:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
format.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = format.parse(text);
long millis = date.getTime();
//your code - save datetime in milliseconds in SQLite numeric field
Indirectly you can avail all features of datetime in SQLite that mysql datetime has.

How to extract time from mysql time then load to datetimepicker with custom hh:mm:ss

I have a datetimepicker with custom format hh:mm:ss tt
I want to fetch the data from mysql with time data type using MySqlDataReader
The date is fine using
dtpmyDate.Value = reader.GetDateTime("dateposted").ToShortDateString
How about the time?
this one resolve me
getTime = reader.GetString(reader.GetOrdinal("timeposted")).ToString
Dim today As DateTime = DateTime.Now.Date
Dim timeToSet As New TimeSpan(20, 20, 30)
dtpmyTime.Value = today + " " + getTime
dtpmyTime is datetimepicker with custom format is hh:mm:ss
with ShowUpDown = true
I only wants the time value since the date will be updating current date to the database when the record is saved..
Thank you everyone. this site is more practicable place to query my problem that will solve immediately.
When you assign the .Value of a DateTimePicker, you need to include the date part as well as the time part. If the MySQL column has a type of TIME, then you should read that as a TimeSpan and add it to an appropriate date.
Dim today As DateTime = DateTime.Now.Date
Dim timeToSet As New TimeSpan(20, 20, 30)
DateTimePicker1.Value = today + timeToSet

How to take Date from sql db as a single result and compare with current date

i have a complex problem with Date field. Describe what i want to do:
I have field date1 as Date in my db.
#Temporal(TemporalType.DATE)
private Date date1;
I want to take data from this field and compare with current date.
#Query("SELECT date1 FROM Table io WHERE io.date1 >= DATE_FORMAT(CURRENT_DATE, '%Y-%m-%e')")
Date findAll2();
public boolean CheckDate1(){
currentDate = new Date();
date1 = getInterimOrdersRepo().findAll2();
if(currentDate.before(date1) || currentDate.equals(date1)){
System.out.println("TRUE");
System.out.println("currentDate = "+currentDate);
return true;
}
else{
System.out.println("FALSE");
return false;
}
}
but i have an error:
result returns more than one elements; nested exception is javax.persistence.NonUniqueResultException
When method return false i want do Update field data1 with " " empty data.
I using jsf, what i must to do?
It seems that you are trying to read several values from the table into a single variable, and that is the error.
findall2 returns an array (most likely) and u should read one of it's values - try reading first one.
Furthermore, I believe that you can skip the "DATE_FORMAT" in your query, and this is a very strange way to write a code. Not clear what u are trying to achieve here

Grails insert date without time

I have a date attribute in my domain and I want to insert to MySQL without time. getting exception cannot cast string to date.Exception message:
Cannot cast object 2013-03-09 with class java.lang.String to class java.util.Date
I want to insert to the database without time.
Domain:
class Day {
Date date
static mappings = {
table name:'Days'
date type: 'date'
}
Controller:
def today = new Date()
def ymdFmt = new java.text.SimpleDateFormat("yyyy-MM-dd")
Date dateYmd = ymdFmt.format(today)
day.date =dateYmd
day.date = new Date().clearTime()
clearTime() will reset the time portion to 00:00:00
I think you wan't to look at Date.parse http://groovy.codehaus.org/groovy-jdk/java/util/Date.html#parse(java.lang.String, java.lang.String)
You can parse the date like.
String stringDate = '28/09/2010'
Date date = new Date().parse('d/M/yyyy', stringDate)
You should use java.sql.Date instead of java.util.Date.
The java.sql.Date class corresponds to SQL DATE which means it stores years, months and days while hour, minute, second and millisecond are ignored. Additionally java.sql.Date isn't tied to timezones.
For a good explanation about the two see java.util.Date vs java.sql.Date

Convert an object to a date

I have imported an xml file that contains a date stamp. I extracted the date stamp by placing it into an Object:
dataObject = new Object();
dataObject.date = ... etc.
The date stamp was created by a sql database and its structure is as follows, but it is no longer a Date:
2011-02-03 16:30:10
Can I convert the text in the Object back into a Date object within Flex to be able to use Date methods?
You can use Date::parse for this:
var date:Date = new Date(Date.parse(dateString));
However, note that Flash doesn't recognize "-" as a separator so you need to convert the string first. Something like this should work:
dateString = dateString.replace(new RegExp(/-/g), '/');
var date:Date = Date.parse(dateString);