What is the proper date format for a given date - java

What is the proper date format for this date instance ..
10/10/2011 2:36:00 PM
I've used this ..
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss a");
edit
more code
Object temp = jsonArray.getJSONObject(i).get("STARTDATE"); // date object from webserive
Date appointment.mStartDate = formatter.parse(temp.toString());
but it returned this date in this format ..
Thu Nov 10 00:36:00 GMT+02:00 2011

but it returned this date in this format ..
Thu Nov 10 00:36:00 GMT+02:00 2011
You were thus doing System.out.println(appointment.mStartDate);? That's then perfectly fine. It's indeed the default format of the Date#toString() method. When you pass a non-String object to System.out.println(), then its toString() method will be called and the returned String will be displayed.
If you want to display it in the same format as you have retrieved it, then you should be using the SimpleDateFormat#format() method to convert Date to a String in the desired format:
String dateString = formatter.format(appointment.mStartDate);
System.out.println(dateString);

Use parseObject
Date appointment.mStartDate = (Date) formatter.parseObject(temp.toString());

There is four acknowledged variants:
US M/D/YY
ISO-8601 YYYY-MM-DD
JIS ?
EUR DD.MM.YYYY

What's your problem? The date appears to have been parsed correctly for a timezone of +2 hours. Naturally, when you simply print the toString description, the displayed value is in the default format -- to format the output you need to do a date formatter operation.

Related

Converting java.util.Date to java.sql.Timestamp results into wrong value

Server side code (server timezone is UTC):-
Date aDate = new Date();
java.sql.Timestamp aTimestamp = new java.sql.Timestamp(aDate.getTime());
Client side (Mobile app, timezone GMT +5:30):-
Hitting a service request which runs above code on server side
The issue is when i debugged on server, found following values :-
aDate.getTime() prints to -> 1470472883877 milliseconds i.e., Sat Aug 06 2016 14:11:23 GMT+0530
but
aTimestamp prints to -> (java.sql.Timestamp) 2016-08-06 08:41:44.109
It's kinda weird, i've no idea what's going on in conversion !! please help
UTC and GMT are formats.
java.util.Date and java.sql.Timestamp are independent of the timezone. They store a long time in ms for representing their inner state.
For information, Calendar is timezone aware.
So with Date or Timestamp, to differentiate GMT or UTC format in an output, you have to use a formater which outputs the date into string by being aware the timezone.
In your output : 2016-08-06 08:41:44.109, you don't use a formater which is aware of the timezone. It's probably the result of a toString() on the java.sql.Timestamp instance or something of similar.
What you consider as a conversion is not a conversion but a formatting since the timestamp stays the same between the two objects.
If you want to display in the UTC format, use the appropriate formater with a
SimpleDateFormat for example :
SimpleDateFormat dt= new SimpleDateFormat("MM/dd/yyyy hh:mm:ss z");
dt.setTimeZone(TimeZone.getTimeZone("UTC"));
String dateStringInUTC = dt.format(new Date(yourSqlTimestamp.getTime()));
The following is probably what you are looking for:
Calendar cal = Calendar.getInstance(Locale.GERMANY); // use your locale here
Timestamp aTimestamp = new Timestamp(cal.getTimeInMillis());
System.out.println(aTimestamp);
System.out.println(cal.getTime());
And the output:
2016-08-06 19:12:54.613
Sat Aug 06 19:12:54 CEST 2016

Formatting a date in java

I have the two Date objects which I am trying to format from being in MM/DD/YYYY format to "yyyy-mm-dd HH:mm:ss" format.
The current approach I am using is to first format those dates using SimpleDateFormat which will return two Strings, then I have to convert this string back to Date to get the formatted final Date objects.
So I was wondering if there was a simpler way to change the Date object format without going in many steps?
Thanks
The format is irrelevant. Date simply represents the number of milliseconds since the Unix epoch.
Remember, Date has no concept of format, it doesn't care.
You should simply format the Date object with whatever formatters you need...
For example...
Date date = new Date();
System.out.println(date);
System.out.println(DateFormat.getDateTimeInstance().format(date));
System.out.println(new SimpleDateFormat("dd/MM/yyyy").format(date));
System.out.println(new SimpleDateFormat("yyyy MMMM EE").format(date));
System.out.println(new SimpleDateFormat("EEEE MMMM yyyy").format(date));
System.out.println(date);
Outputs...
Wed Jan 22 11:55:18 EST 2014
22/01/2014 11:55:18 AM
22/01/2014
2014 January Wed
Wednesday January 2014
Wed Jan 22 11:55:18 EST 2014
Note how the first and last values don't change. Date has no internal concept of format, that's the responsibility of the formatter.
For example, if I took the String value 22/01/2014 and parsed it back to a Date using SimpleDateFormat
Date date = new SimpleDateFormat("dd/MM/yyyy").parse("22/01/2014");
And then outputted the date value...
System.out.println(date);
It would output something like...
Wed Jan 22 00:00:00 EST 2014
The format has being lost. It would need to use an appropriate formatter to change what is displayed

Can't make java.text.SimpleDateFormat to work

I have a Date object as follows:
java.util.Date d = new java.util.Date(System.currentTimeMillis()); // Mon Dec 23 14:57:28 PST 2013
I need to format the date to get another Date object with this format instead:
2013-12-23 14:57:28
I tried this code:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
sdf.format(d); // d is still Mon Dec 23 14:57:28 PST 2013, no formatting.
I tried this code:
String s = d.toString();
try {
d = sdf.parse(s);
} catch (Exception e)
e.printStackTrace(); // java.text.ParseException: Unparseable date: "Mon Dec 23 14:35:48 PST 2013"
Would you please tell me what am I doing wrong? I googled searched it but the solutions to format a Date was more or less what I tried. Any help is greatly appreciated.
You don't understand what a Date is, and what format() does. A Date is just a number of milliseconds. Nothing more. It doesn't have any format. Formatting a date doesn't change the date at all. It returns a string containing a human readable representation of the date (like "2012-11-23" or "Monday, April 2").
So, the following instruction:
sdf.format(d);
is effectively a noop. You ignore the string that it returns.
If what you want is to have a specific format used when calling date.toString(), it's impossible. When you want to display a date in a specific format (yyyy-MM-dd for example), instead of doing
System.out.println(date);
use
DateFormat format = new SimpleDaeFormat("yyyy-MM-dd");
System.out.println(format.format(date));
All this is clearly explained in the javadoc. You should read it.
SimpleDateFormat, does not change the date format, it gives you a formatted date for display purpose only, not for anything else.
util.Date will always have one format (a long number of milliseconds) that you can format to any way you want in order to display using SimpleDateFormat. So in effect no matter what date you get you can format to what format you want.
If you explain why you are trying to do what you are trying to do, then maybe we can support you better.

Parsing date gives Date of different format to what is specified. (Java)

I get dates as Strings (ie: 2013-04-07 17:20:16.0) and I need to create Date objects to represent these so that I can set the date's in JSpinners.
I am using this to format the date strings I get:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
I am using it like this:
df.setLenient(false);
Date tempDateOld = new Date();
tempDateOld = df.parse("2013-04-07 17:20:16.0");
However this:
System.out.println(tempDateOld.toString());
gives this:
Sun Apr 07 17:20:16 CAT 2013
Why does it not just give me a Date with the date in the same format? How do I take a date of a given format and create a Date object with the date of the same format.
Any help will save my sanity, thanks.
Try
System.out.println(df.format(tempDateOld));
The date by itself does not keep the format.

Date Format Confusion

in my App User Selects Date like 2013-01-02
but in MySql DB the column format is like 02-Jan-2013 so want change the Date Format
my code is:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
SimpleDateFormat formatter1 = new SimpleDateFormat("dd-mmm-yyyy");
Date start_date = (Date)formatter.parse(GBRF.getStart_date());//GBRF.getStart_date() returns Form Data as 2013-01-02
String dt=formatter1.format(start_date);
Date sd=(Date)formatter1.parse(dt);
at the End sd print Date like this Wed Jan 02 00:01:00 IST 2013
i dont want that format..
just i want like 02-Jan-2013
give me an idea..
Thanks in Advance
You can format Date into String with SimpleDateFormat, after that if you try to print date instance it will invoke toString() method of Date class which has no change in output and you can't alter that output because it is coming from toString() implmentation
Note: in your format you need to use M for month (note capital M)
A Date object does not have a format in itself. It holds just a point in time. The format comes into play when you convert it to or from a String.
Also note that m in the format is used for minutes not months. So you probably want a format like dd-MMM-yyyy.
Try
System.out.println(formatter1.format(sd));
to get the date printed as you like it.
You need to change your date format first :
`SimpleDateFormat formatter1 = new SimpleDateFormat("dd-MMM-yyyy");`
In your case, if you just print dt, it should print date in the required format instead of converting it into Date object again i.e. sd
When you are printing sd, without any formatting it will print the default String implementation of Date object which is exactly what you are seeing as an output

Categories