If I have a method like this:
public static String convertDateTimeToString(DateTime dt) {
return dt.getDate() + " " + dt.getTime();
}
Which takes a DateTime object of my own which contains a java.sql.Date and a java.sql.Time, what is the best way of reversing the process so that I can substring a java.sql.Date and a java.sql.Time from a String?
Or if DateTime dt is a JodaTime DateTime object?
If this can be done without reference to java.util.Date.
Look at SimpleDateFormat class. Here's a tutorial.
You'll need something like this:
String datetimeString;
Date result;
SimpleDateFormat formatter = new SimpleDateFormat("EEE d MMM yy hh:mm:ss");
result = formatter.parse (datetimeString);
You can use the valueOf method of java.sql.Time to convert a String into a java.sql.Time, and use the valueOf method of java.sql.Date to convert a String into a java.sql.Date
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);
}
I want to convert java.util.Date to java.sql.Date but I want hours, minutes, and seconds as well but java.sql.Date can be used only to store date(no time) . I tried the below code but it is giving only year, month, and day for the java.sql.Date object.
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
Date parsed = format.parse("20110210120534");
System.out.println(format.parse("20110210120534"));
java.sql.Date sql = new java.sql.Date(parsed.getTime());
System.out.println("SQL date is= "+sql);
Current output:
2011-02-10
Desired output:
2011-02-10 12:05:34
The java.sql.Date type is used to store only date (no time) information, as it maps to the SQL DATE type, which doesn't store time. What its toString() method does is:
Formats a date in the date escape format yyyy-mm-dd.
To achieve the desired output you can use java.sql.Timestamp, which stores date and time information, mapping to the SQL TIMESTAMP type. Its toString() method outputs what you need:
Formats a timestamp in JDBC timestamp escape format: yyyy-mm-dd hh:mm:ss.fffffffff, where ffffffffff indicates nanoseconds.
Example:
java.text.DateFormat format = new java.text.SimpleDateFormat("yyyyMMddHHmmss");
java.util.Date date = format.parse("20110210120534");
java.sql.Timestamp timestamp = new java.sql.Timestamp(date.getTime());
System.out.println(timestamp); // prints "2011-02-10 12:05:34.0"
As other folks said, you need to use java.sql.TimeStamp.
public class Test {
public static void main(String[] args) {
java.util.Date date = new java.util.Date();
java.sql.Timestamp sqlTimeStamp = new java.sql.Timestamp(date.getTime());
System.out.println("util-date:" + date);
System.out.println("sql-timestamp:" + sqlTimeStamp );
}
}
http://tutorials.jenkov.com/java-date-time/java-sql-date.html
I have some code that uses the sql.date object to get the current date in the format: yyyy-MM-dd. I want to typecast that to a Calendar object that has the same format as the sql.date.
Here is the code for the sql.date:
java.util.Date now = new java.util.Date();
java.sql.Date sqlDate = new java.sql.Date( now.getTime() );
Edit:
I want to know how to put this in a Calendar object. Is it even possible to do this?
A java.sql.Date doesn't have a format. It's just a date.
To convert it to a string, use a SimpleDateFormatter with the relevant format set - or just use toString if you definitely want yyyy-MM-dd. (Unfortunately it's unclear which time zone you should use - java.sql.Date is very poorly documented in this respect. I suspect it will use the default system time zone.)
To create a Calendar object with the given date, you can just use:
Calendar calendar = Calendar.getInstance();
calendar.setTime(sqlDate);
Again, a Calendar doesn't have a text format either - it represents a point in time in a particular calendar system in a particular time zone, but nothing about a textual representation.
Use SimpleDateFormat#format()
System.out.println(new SimpleDateFormat("yyyy-MM-dd")
.format(Calendar.getInstance().getTime()));
Java Date objects (and note that java.sql.Date is a java.util.Date) don't have "formats". They represent an instant in time.
Calendar also doesn't have a "format".
What you're asking for doesn't make sense in java.
Consider using SimpleDateFormat to format one of these things into a String:
String str = new SimpleDateFormat("yyyy-MM-dd").format(myDate);
or if you use a Calendar object (not recommended):
String str = new SimpleDateFormat("yyyy-MM-dd").format(myCalendar.getTime());
You can use the following snippet.
public class DateTest {
public static void main(String[] arg){
Calendar calendar = Calendar.getInstance();
//setting a valid date
Date date = new Date(113,07,10);
//prints the date as per format
System.out.println("Date in YYYY-MM-DD : " +date);
//sets the calendar with the date value in java.sql.Date instance
calendar.setTime(date);
//use simple date formatter to format the value in calendar instance
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("Date formatted from Calendar instance : "+dateFormat.format(calendar.getTime()));
}
}
Please let me know what happens!
String str = "13/06/2011";
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date date = (Date)formatter.parse(str);
I guess that your Date class is actually a java.sql.Date.
What does your import statement say? Are you importing some other class (for example java.sql.Date) by accident? What does the compiler say when you remove the class cast (which should not be there)?
DateFormat.parse() returns an instance of java.util.Date and not java.sql.Date.
In order to convert from java.util.Date to java.sql.Date, I do the following:
java.util.Date fromDate = df.parse(fromdate1);
java.sql.Date sqlDate = new java.sql.Date(fromDate.getTime());
This question already has answers here:
How to convert ISO 8601 date (string) to Date?
(3 answers)
Closed 5 years ago.
How can I convert a String object to a Date object?
I think I need to do something like this:
Date d=(some conversion ) "String "
Any help would be greatly appreciated.
SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
Date date = dateFormat.parse("1.1.2001");
For details refer to: SimpleDateFormat documentation
Date-to-String conversion is a relatively complex parsing operation, not something you can do with a simple cast as you are trying.
You'll have to use a DateFormat. It can be as simple as:
Date d = DateFormat.getDateInstance().parse("09/10/2009");
But this changes the expected date format depending on the locale settings of the machine it's running on. If you have a specific date format, you can use SimpleDateFormat:
Date d = new SimpleDateFormat("d MMM yyyy HH:mm").parse("4 Jul 2001 12:08");
Note that the parse method will always expect one specific format, and will not try to guess what could be meant if it receives a different format.
See Sun's Java tutorial and the class SimpleDateFormat
Use a SimpleDateFormat with a format string, which matches your actual format:
SimpleDateFormat sdf =
new SimpleDateFormat("yyyy-MM-dd");
Date d = sdf.parse("2009-10-09");
java.text.SimpleDateFormat that extends java.text.DateFormat abstract class.
DateFormat MYDate = new SimpleDateFormat("dd/MM/yyyy");
Date today = MYDate.parse("09/10/2009");
you should parse the string with the SimpleDateFormat class
use
Date date = DateFormat.getInstance.parse( dateString );
You can convert String object into Date object using this method. and this Java code is tested and running component in my environment.
public static Date parseStringAsDate(String dateStr, String format) throws ParseException
{
if(null==dateStr || "".equals(dateStr))
throw new IllegalArgumentException("dateStr must not be null or empty");
DateFormat df = new SimpleDateFormat(format);
return df.parse(dateStr);
}
dateStr = "17/05/2017"
format= "dd/MM/yyyy"