The incredible java time machine [duplicate] - java

This question already has answers here:
java parsing string to date
(2 answers)
Closed 9 years ago.
What happens to my time/date using this sample code??
package date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class DateFormatTest
{
public static void main(String args[]) throws ParseException
{
final String pattern = "dd/MM/YYYY HH:mm";
final Locale locale = Locale.FRENCH;
final SimpleDateFormat formatter = new SimpleDateFormat(pattern, locale);
Date d = new Date();
System.out.println("Today: " + d);
String parsedDate = formatter.format(d);
System.out.println("Today as string:" + parsedDate);
Date d2 = formatter.parse(parsedDate);
System.out.println("Today parsed back:" + d2);
}
}
Output:
Today: Fri Jun 28 16:28:04 CEST 2013
Today as string:28/06/2013 16:28
Today parsed back:Mon Dec 31 16:28:00 CET 2012 >>> ????

pattern = "dd/MM/YYYY HH:mm";
should be
pattern = "dd/MM/yyyy HH:mm";
See JavaDoc.
But note that this code as you posted does not even run on my Eclipse:
java.lang.IllegalArgumentException: Illegal pattern character 'Y'
Ah, Y is added in Java 7. But it is weekyear.

Little explanation, but is only a guessing, correct me if I'm wrong.
As the explanation of week year I guess that parsing the week year of 2013 (due to the wrong pattern 2013 -> YYYY ) is somehow setting the whole date to the first week year of the 2013, that is Monday 31/12/2012.

Related

SimpleDateFormat object changes timezone without adding/subtracting the correct number of hours [duplicate]

This question already has answers here:
parsing date/time to localtimezone
(2 answers)
How to Parse Date from GMT TimeZone to IST TimeZone and Vice Versa in android
(4 answers)
SimpleDateFormat returns wrong time zone during parse
(2 answers)
Java SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") gives timezone as IST
(9 answers)
Closed 2 years ago.
I have a String which represents a Date in the UTC timezone (because my database uses UTC). I want to convert this String into a date with SimpleDateFormat. The problem is that converts it into a Date in the CEST timezone without adding the 2 hour separating UTC and CEST. Here is the code:
//This is a date in UTC
String text = "2020-09-24T09:45:22.806Z";
//Here I define the correct format (The final Z means that it's UTC)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
//Then I parse it
Date date = sdf.parse(text);
//Then I print it
System.out.println(date);
The result of the print is
Thu Sep 24 09:45:22 CEST 2020
Why CEST? I would like it to remain UTC, but if it has to become CEST at least add the 2 hours
You should setTimeZone() to your DateFormat like
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class Main {
public static void main(String[] args) throws ParseException {
//This is a date in UTC
String text = "2020-09-24T09:45:22.806Z";
//Here I define the correct format (The final Z means that it's UTC)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
//Then I parse it
Date date = sdf.parse(text);
//Then I print it
System.out.println(date);
}
}
I also replaced 'Z' to X following the documentation

What should be the format in SimpleDateFormat to print date like this - 01-JUN-20 08.55.27.577984000 AM UTC? [duplicate]

This question already has answers here:
How to get the current time in YYYY-MM-DD HH:MI:Sec.Millisecond format in Java?
(16 answers)
How to get the current date/time in Java [duplicate]
(28 answers)
How to format a ZonedDateTime to a String?
(3 answers)
Closed 2 years ago.
I want to print the current date in this format -
01-JUN-20 08.55.27.577984000 AM UTC. Tried lot of formats in SimpleDateformat but none is working. What is the right way to print like this?
SimpleDateFormat
You can use the following pattern:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class TimeFormatting {
public static void main(String[] args) {
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yy hh.mm.ss.SSS a Z");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
String formatted = formatter.format(new Date(2015, 4, 4, 13, 7, 19)); //One example date
System.out.println(formatted);
}
}
This will give the output 04-05-15 11.07.19.000 AM +0000 where +0000 equals to UTC.
Explanation of the pattern dd-MM-yy hh.mm.ss.SSS a Z:
dd = the day
MM = the month
yy = the year
hh = the hour
mm = the minute
ss = the second
SSS = the millisecond. There does not exist a reference to nanoseconds in SimpleDateFormat (see this answer).
a = to show AM/PM
Z = to show the timezone
Additional information
Please note: SimpleDateFormat is deprecated. You should use DateTimeFormatter. One big advantage is that you can also have the nanoseconds (n).
One example:
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.ZoneOffset;
public class TimeFormatting {
public static void main(String[] args) {
ZonedDateTime time = ZonedDateTime.now(ZoneOffset.UTC);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yy hh.mm.ss.n a 'UTC'").withZone(ZoneOffset.UTC);
String timeDate = time.format(formatter);
System.out.println(timeDate);
}
}
Edit:
If you want to have UTC instead of +0000 with SimpleDateFormat you can use dd-MM-yy hh.mm.ss.SSS a zzz instead.

java XMLGregorianCalendar shows different year on conversion [duplicate]

This question already has answers here:
Why does sdf.format(date) converts 2018-12-30 to 2019-12-30 in java? [duplicate]
(3 answers)
Java SimpleDateFormat returning wrong value in Date object
(1 answer)
Y returns 2012 while y returns 2011 in SimpleDateFormat
(5 answers)
Closed 3 years ago.
Simple test case below is giving results different than expected.
import javax.xml.datatype.XMLGregorianCalendar;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Main {
public static void main(String[] args) {
XMLGregorianCalendar xmlDate = new XMLGregorianCalendarImpl();
xmlDate.setMonth(12);
xmlDate.setDay(31);
xmlDate.setYear(2019);
xmlDate.setHour(0);
xmlDate.setMinute(0);
xmlDate.setSecond(0);
Calendar calendar = xmlDate.toGregorianCalendar();
SimpleDateFormat formatter = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
Date dt = calendar.getTime();
String ds1 = dt.toString();
System.out.println("ds1 = " + ds1);
String dateString = formatter.format(calendar.getTime());
System.out.println("dateString = " + dateString );
}
}
I cannot figure out why the year component of dateString is showing as 2020 instead of 2019.
ds1 = Tue Dec 31 00:00:00 EST 2019
dateString = 2020-12-31 00:00:00
Please change your code to
import javax.xml.datatype.XMLGregorianCalendar;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Main {
public static void main(String[] args) {
XMLGregorianCalendar xmlDate = new XMLGregorianCalendarImpl();
xmlDate.setMonth(12);
xmlDate.setDay(31);
xmlDate.setYear(2019);
xmlDate.setHour(0);
xmlDate.setMinute(0);
xmlDate.setSecond(0);
Calendar calendar = xmlDate.toGregorianCalendar();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dt = calendar.getTime();
String ds1 = dt.toString();
System.out.println("ds1 = " + ds1);
String dateString = formatter.format(calendar.getTime());
System.out.println("dateString = " + dateString );
}
}
As YYYY represents year of the week and yyyy represents calendar year in simple date format.
More explanation here: Java SimpleDateFormat shifts Date by one year

Unparseable Date Or Wrong date

Hi I know there are many post on this but I can't figure out what's happening. I'm getting a date as a String from db and trying to convert it to another form.
Didn't understand that had to FIRST parse the string and then use another SimpleDateFormat to Format that date. I know get what I expected when printed.
Date one: Mon Jun 16 04:00:00 EDT 2014
06-16-2014
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class DateCheck {
public static void main(String[] args) {
try {
String str = "2014-06-16 04:00:00.0";
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date1 = df.parse(str);
System.out.println(" Date one: " + date1);
SimpleDateFormat df2 = new SimpleDateFormat("MM-dd-yyyy");
System.out.println(df2.format(date1));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
2 things to note, if i uncomment out the setLenient method i get unparseable date exception.
If commented out i get:Mon Oct 06 00:00:00 EDT 2183
Any help?
EDIT: Sorry that was just stupid. I know get Thu Jan 16 00:06:00 EST 2014
which is correct but i wanted it like 01-16-14
Your format doesn't match your input String.
The input String is in the format of "yyyy-MM-dd hh:mm.s" (there is an assumption on the format as we don't have enough information to make an accurate prediction)
String str = "2014-06-09 00:00.0";
DateFormat df = new SimpleDateFormat("MM-dd-yy");
df.setLenient(true);
Date result = df.parse(str);
Which outputs
Mon Jun 09 00:00:00 EST 2014
So either your format is wrong or the value in the database is wrong. This will depended on your requirements

Converting string to a formatted date

I am trying to convert a string to proper date format using Java's SimpleDateFormat. For some reason, it's not working with certain months like "Mar", "May", "Oct", and "Dec." Can somebody help me? It works fine for all other months.
import java.sql.Date;
import java.text.SimpleDateFormat;
import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;
public class test {
public static void main(String args[]) throws java.text.ParseException {
try {
SimpleDateFormat parse = new SimpleDateFormat("dd. MMM yyyy hh:mm:ss");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//why this doesn't work with certain months like Mar, May, Oct, and Dec? otherwise it works fine
String dateTime = "01. Jun 2010 15:30:32";
//String dateTime = "07. Mar 2011 15:20:10";
//String dateTime = "07. May 2011 15:20:10";
//String dateTime = "07. Oct 2011 15:20:10";
//String dateTime = "07. Dec 2011 15:20:10";
java.util.Date parsed =parse.parse(dateTime);
System.out.println("formatted: " + formatter.format(parsed));
} catch(ParseException e) {
System.out.println("Caught " + e);
}
}
}
You need to set the locale on SimpleDateFormat, otherwise the platform default locale will be used for month names. You can do that by passing it as 2nd argument to the SimpleDateFormat constructor. If you want to work with English formatted month names, pass Locale.ENGLISH.
new SimpleDateFormat("dd. MMM yyyy hh:mm:ss", Locale.ENGLISH);
By the way, you can learn about your platform default locale by
System.out.println(Locale.getDefault());
This is configureable at OS level (in Windows control panel, for example) and as JVM argument.

Categories