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
Related
The below gives: 1475020875000. When I convert this epoch back to a human readable timestamp, I get: Wed, 28 Sep 2016 00:01:15 GMT, which is different from the initial date?
String date = "2016-09-27 20:01:15.0";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long epoch = df.parse(date).getTime();
System.out.println(epoch);
You should specify Timezone for both input and output. You can use "z" to instantiate SimpleDateFormat and setTimeZone before using format method:
package stackoverflow;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class Programa {
public static void main(String[] args) throws ParseException {
String date = "2016-09-27 20:01:15 GMT";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
long epoch = df.parse(date).getTime();
System.out.println(epoch);
Date d = new Date(epoch);
df.setTimeZone(TimeZone.getTimeZone("GMT"));
String out = df.format(d);
System.out.println(out);
}
}
For available Timezones, try TimeZone.getAvailableIDs()
Worked fine here:
package stackoverflow;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Programa {
public static void main(String[] args) throws ParseException {
String date = "2016-09-27 20:01:15.0";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long epoch = df.parse(date).getTime();
System.out.println(epoch);
Date d = new Date(epoch);
String out = df.format(d);
System.out.println(out);
}
}
You certainly forgot to consider the Timezone to use. If you do not define it, the default (from the JVM) is taken, and can for example differ depending on your server.
Try LocalDateTime
String date = "2016-09-27 20:01:15";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long epoch = df.parse(date).getTime();
System.out.println(epoch);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
LocalDateTime ld = LocalDateTime.parse(date, formatter);
long epoch2 = ld.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
System.out.println(epoch2);
Instant in = Instant.ofEpochMilli(epoch2);
LocalDateTime ldt = LocalDateTime.ofInstant(in, ZoneId.systemDefault());
System.out.println(ldt);
output
1474986675000
1474986675000
2016-09-27T20:01:15
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 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)));
}
}
This question is a duplicate of this question by intention. It seems like the older one is "ignored", while none of the answers there is the answer.
I need to parse a given date with a given date pattern/format.
I have this code, which is supposed to work:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main
{
public static void main(String[] args)
{
final Date date = string_to_date("EEE MMM dd HH:mm:ss zzz yyyy",
"Thu Aug 14 16:45:37 UTC 2011");
System.out.println(date);
}
private static Date string_to_date(final String date_format,
final String textual_date)
{
Date ret = null;
final SimpleDateFormat date_formatter = new SimpleDateFormat(
date_format);
try
{
ret = date_formatter.parse(textual_date);
}
catch (ParseException e)
{
e.printStackTrace();
}
return ret;
}
}
For some reason I'm getting this output:
java.text.ParseException: Unparseable date: "Thu Aug 14 16:45:37 UTC 2011"
at java.text.DateFormat.parse(DateFormat.java:337)
at Main.string_to_date(Main.java:24)
at Main.main(Main.java:10)
null
What's wrong with my date pattern? This seems to be a mystery.
Your platform default locale is apparently not English. Thu and Aug are English. You need to explicitly specify the Locale as 2nd argument in the constructor of SimpleDateFormat:
final SimpleDateFormat date_formatter =
new SimpleDateFormat(date_format, Locale.ENGLISH); // <--- Look, with locale.
Without it, the platform default locale will be used instead to parse day/month names. You can learn about your platform default locale by Locale#getDefault() the following way:
System.out.println(Locale.getDefault());
This should parse the given string.
public static void main(String[] args) throws ParseException
{
String sd = "Thu Aug 14 16:45:37 UTC 2011";
String dp = "EEE MMM dd HH:mm:ss zzz yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(dp);
Date d = sdf.parse(sd);
System.out.println(d);
}
I should have specify a locale, like this:
final SimpleDateFormat date_formatter = new SimpleDateFormat(date_format, Locale.ENGLISH);
Thanks to BalusC with his great answer!