I have a website which supplies date in 2 formats: 28th June 2009 or June 2009.
Now I would like to convert both of these into the same format yyyy-mm-dd hh:mm:ss using MySQL and Java.
SimpleDateFormat gives an error: "Unparsable Date". What's the solution?
What about June 2009 as you can not say its a date you need to make it a date by adding a day in this month-year format. Ex.. add first day of month here and make it 1 June 2009 then parse it in desired format.
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main(String[] args) throws IOException, ParseException
{
String dateStr = "28 June 2009";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println(sdf.format(new Date(dateStr)));
}
}
Related
Here, I attached my code and finally I get the date in string format (ex: 1). But I want it for date format (ex: 2)
Ex:1
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class Example{
public static void main(String []args){
java.util.Date date = new Date("Sat Dec 01 00:00:00 GMT 2017");
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
String formatz = formatter.format(date);
System.out.println(formatz);
}}
The O/P is:
12/01/2017 // String so i cant able to parse it on my query.
Ex:2
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class Example{
public static void main(String []args){
java.util.Date date = new Date("Sat Dec 01 00:00:00 GMT 2017");
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
String formatz = formatter.format(date);
Date d1 = formatter.parse(formatz);
System.out.println(d1);
}}
The O/P is:
Sat Dec 01 00:00:00 GMT 2017 // but i want 12/01/2017 (date format)
how can i resolve it.
try this:
System.out.println(formatter.format(d1));
First of all, I cannot more strongly advise against using the legacy java.util.Date and java.util.Calendar. You should always use the appropriate class in the java.time package instead.
Now, onto your question. Neither java.util.Date, nor any of the java.time.* classes, contain information about formatting. They contain simply enough information to represent the moment in time. If you want to use a different format from the default, you will need to use either a built-in formatter, or your own.
In your case, because formatz already has the format you want, you can simply print that instead.
is this possible to get the output in Date type in the below format
> 2014-11-12 09:23:47 GMT+05:30
not to be like
> Wed Nov 12 06:53:47 IST 2014
That can be done using SimpleDateFormat with the format string:
yyyy-MM-dd HH:mm:ss 'GMT'XXX
as per the following program:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main(String[] args) {
Date dt1 = new Date();
DateFormat df = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss 'GMT'XXX");
String line = df.format(dt1);
System.out.println(line);
}
}
On my system, that gives me:
2014-11-14 15:36:16 GMT+08:00
I am running into some odd output using the Java, DateFormat object. For some reason it is adding one to my month and I am not sure why. I have broken down the problem as simple as possible.
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
*
* #author djc39_000
*/
public class TestDate {
public static void main(String[] args) {
DateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
Date testDate;
try {
testDate = (Date) formatter.parse("12/6/2013 11:23:04 AM");
System.out.println(testDate);
} catch (ParseException ex) {
System.out.println(ex);
}
}
}
Output:
Sun Jan 06 11:23:04 EST 2013
Expecting Output:
Fri Dec 06 11:23:04 EST 2013
Also, if I change the month to 11 in my string it does not change the month in the stamp. What am I doing wrong? Thanks
Solution was found, mm is for mins, and I used for month which should have been MM.
Are there any other characters that might be easily confused for bonus points?
Date format should be "MM/dd/yyyy hh:mm:ss a"
I am having a problem using the parseDateTime method in joda time. When I try to parse the date below, the result is one day off. I know there is already a similar thread about this, and I know that if your dayOfWeek and dayOfMonth are mismatched, it prioritizes the dayOfWeek. But my date is valid -- I have checked that february 22 falls on a Friday. But when I parse it, I am getting thursday, february 21. Here is the code:
DateTimeFormatter NBSfmt = DateTimeFormat.forPattern("EEE, dd MMM yyyy HH:mm:ss Z");
DateTimeFormatter MYfmt = DateTimeFormat.forPattern("yyyy-MM-dd");
String date ="Fri, 22 Feb 2013 00:00:00 +0000";
DateTime datetime = NBSfmt.parseDateTime(date);
System.out.println(datetime.toString());
And here is the output:
2013-02-21T19:00:00.000-05:00
Anyone have any idea what is going on here? Any insight would be greatly appreciated.
Thanks,
Paul
This is caused by your timezone. You define it in +0000 but then you're viewing it in -05:00. That makes it appear one day before. If you normalize it to UTC, it should be the same.
Try this code, as evidence:
package com.sandbox;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
public class Sandbox {
public static void main(String[] args) {
DateTimeFormatter NBSfmt = DateTimeFormat.forPattern("EEE, dd MMM yyyy HH:mm:ss Z");
String date = "Fri, 22 Feb 2013 00:00:00 -0500";
DateTime datetime = NBSfmt.parseDateTime(date);
System.out.println(datetime.toString());
}
}
For you, this should show the "right day". But for me, it shows 2013-02-21T21:00:00.000-08:00 because I'm in a different timezone than you. The same situation is happening to you in your original code.
Here's how you can print the string out in UTC:
package com.sandbox;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
public class Sandbox {
public static void main(String[] args) {
DateTimeFormatter NBSfmt = DateTimeFormat.forPattern("EEE, dd MMM yyyy HH:mm:ss Z");
String date = "Fri, 22 Feb 2013 00:00:00 +0000";
DateTime datetime = NBSfmt.parseDateTime(date);
System.out.println(datetime.toDateTime(DateTimeZone.UTC).toString());
}
}
This prints 2013-02-22T00:00:00.000Z.
The timezone of yours is -5, and joda treats the input as UTC in the example. You can use withZone to get a new formatter if needed.
I am doing something like this in my program :
Calendar cal = DatatypeConverter.parseDateTime("2012-05-29T11:17:04.805-07:00");
System.out.println(cal.getTime().toString());
o/p:
Tue May 29 13:17:04 CDT 2012
Why is the result showing time of 13:17:04, in the input I have given 11:17:04 and time zone -07:00 which is pacific time zone. Should it not print out 11:17:04 ?
Your timezone - the default one when the program is running is different from the timezone given to the DatatypeConverter.parseDateTime() method and the cal.getTime().toString() method used the default timezone to format the date.
Never use Date.toString() to format Date - a Date only knows the milliseconds from the Epoch time. Instead use java.text.SimpleDateFormat like this:
SimpleDateFormat("yyyy-MM-dd HH:mm:ss z").format(dateObject).
import java.util.Calendar;
import java.util.TimeZone;
import java.text.SimpleDateFormat;
import javax.xml.bind.DatatypeConverter;
class TestDate
{
public static void main(String[] args)
{
Calendar cal = DatatypeConverter.parseDateTime("2012-05-29T11:17:04.805-07:00");
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z");
df.setTimeZone(TimeZone.getTimeZone("GMT-07:00"));
String date = df.format(cal.getTime());
System.out.println(date);
}
}