So I'm building a simple app which shows a list of dates in a ListView.
The dates are being fetched from a database in JSON format.
My database has a column 'AttendanceDate' of 'date' data type.
I think the problem is with 'date' data type of SQL. If so, how do I convert the date ( in 'date') to date ( in 'String').
I tried this but it didn't work:
jsonObject.getString("AttendanceDate").toString();
I also tried to typecast the date to string but that too didn't work:
String date = (String) jsonObject.get("AttemdanceDate");
Link to Json string: http://sasananshul.16mb.com/test.php
Below I have given a code snippet that could help you to change the Date to String format.
//Creating Date in java with today's date.
Date dateNow = new Date();
//change date into string yyyyMMdd format example "20110914"
SimpleDateFormat dateformatyyyyMMdd = new SimpleDateFormat("yyyyMMdd");
String date_to_string = dateformatyyyyMMdd.format(dateNow);
System.out.println("date into yyyyMMdd format: " + date_to_string);
Related
I am working the Talend studio tool for data migration. Now I want to set the Current DateTime in the Date field.
I get the DateTime from this code TalendDate.getDate("yyyy-MM-dd HH:mm:ss") but it returns String type data. But I need Date type to insert.Is there any String to date (Sample insert is like this:1999-12-13 16:14:48) conversion is in the Talend Studio.
You can use routine function TalendDate.parseDate to convert a String to a Date.
TalendDate.parseDate("yyyy-MM-dd HH:mm:ss", yourStringData);
If you want the current datetime:
TalendDate.parseDate("yyyy-MM-dd HH:mm:ss", TalendDate.getDate("yyyy-MM-dd HH:mm:ss"));
But this makes no sense. Parsedate function is prepared to receive a string and convert it to a Date object. Date objects have it's own format, so you don't have to care how is stored, you need to change the Date format in the moment you show it, but not when you store it:
// this will produce a correct Date Object to store in your Date field
Date currentDate = TalendDate.getCurrentDate();
When you need to show/print it use SimpleDateFormat for example if you want to show 2015-07-05 16:00:00 you must do like this:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss);
System.out.println("My date formatted is: " + sdf.format(currentDate ));
its very simple with the use of DateFormat in java
public static void convert(String inputDate) throws ParseException {
DateFormat format = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
Date d = format.parse(inputDate); // example 1999-12-13 16:14:48
System.out.println(d);
}
After getting date from MYSQL through results in java in date format how to compare it with the date taken as string from a jtextbox in java netbeans ?
depends on the date format which in the TextField you can get the date using DateFormat like this:
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");//put prefer format indide it
Date d1 = format.parse(jtextfielddate.getText());
now you can compare it with the date which you retrieved from database(Date d=rs.getDate("date");) easily:
if(d1==d)
{
//do something
}
I am having a string value in format "2013-11-19 07:41:38.990000000". I want this value to be converted into a timestamp field in "19-NOV-13 07.41.38.000000000 AM" format in my oracle database.
I have tried to convert it using the following code:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
Date parsedDate = dateFormat.parse(faxRecd);
timestamp = new Timestamp(parsedDate.getTime());
But while inserting it is generating the following error:
ORA-01843: not a valid month
You could try checking the date format on each database to ensuer they are the same
Code:
select * from nls_session_parameters
This question already has answers here:
How can I get Date in MM/DD/YY format from Timestamp
(7 answers)
Closed 9 years ago.
How to convert java.util.Date directly into desired Date format ?
I have tried to convert the date into string first and then again into date but it still takes date format as Mar 10,2011, i want this format as 10-03-2011
Its simple :) and SimpleDateFormat can help you out.
But if you have datestring then yes you need to parse the datestring to date and re-format it again to desired format(dd-MM-yyyy).
OP failed to mention that he is displaying these in a JTable. You need these things for your date column in your JTable.
A CellRenderer : This will display the date in its string format (Use Simpledateformat)
A TableModel : This holds the internal representation
Now when you sort the JTable, the column will sort on the internal data representation.
Just pass in your date object directy to this method and you'll get the date in desired string format
new SimpleDateFormat("dd-MM-yyyy").format(date);
You can do like this
SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy");
String date = format.format(Date.parse(objectDate.toString()));
Create a SimpleDateFormat with the date format dd-MM-yyyy:
Date today = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
String date = dateFormat.format(today);
System.out.println("Today in dd-MM-yyyy format : " + date);
try this code.
String dateFormat = "dd-MM-yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, new Locale("en_US"));
String tDate = sdf.format(date);
System.out.println(tDate);
You can try this
Date date = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).parse(string);
for 10-03-2011 your format should be "dd-MM-yyyy"
i need to compare two dates
task.deadline have Date data type but when i get system date it gives me in string how to get system date in Date data type
if (!task.deadline.before(systemDate)) {
// add row to to do tab
model_1.addRow(row);
} else {
// add row to done tab
model_2.addRow(row);
}
Parse the systemDate to a Date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date sysDate = sdf.parse(systemDate);
Now you can use sysDate in the comparison.