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
Related
I'm trying to upload the current UTC time to a date "field" in Parse. How can I do this?
DateFormat df = DateFormat.getTimeInstance();
df.setTimeZone(TimeZone.getTimeZone("utc"));
String utcTime = df.format(new Date());
message.put("lastReplyUpdatedAt", utcTime);
message.saveInBackground();
Unfortunately, I can't seem to get the right imports to work for the classes above so I can't construct my String. What can I do here? Also I'm not sure if Parse would even accept that String. Are there any special formats I need to consider?
Pass a Date
From the Parse platform documentation, pass a Date object rather than a String.
Date myDate = new Date();
message.put("lastReplyUpdatedAt", myDate);
message.saveInBackground();
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.
Okay, so here's my issue in Android right now. On our Database there's a timestamp in this format 8/15/2013 2:00:48 PM and through a .NET WebService I get that same time like this in Android: 2013-08-15T14:00:48-07:00. Now I want to convert this format into a Date Time format that I can use for comparison (for example this webservice provides every instance where a device failed at logging in so we want to check the amount of time between occurances to see if there's any issues). Below I have this code where I'm trying to use JODA Time but it's still not returning the correct format:
public static Date convertStringToDate(String input) {
String pattern = "yyyy-MM-dd'T'HH:mm:ssZ";
DateTimeFormatter formatter = DateTimeFormat.forPattern(pattern);
DateTime dateTime = formatter.parseDateTime(input);
return dateTime.toDate();
//printout shows: Thu Aug 15 17:00:48 EDT 2013
}
I know that the server is returning some crappy time format that is hard to work with (it took a while to get this to work in the iOS App we have, and even there it's still rather clunky) so I don't mind changing the webservice or the query if that would make things easier.
I have a very similar format, and I parse it using SimpleDateFormat, try this:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZ", Locale.US);
Date dateTime = format .parse(value);
What i understand is that you have your correct instance of date already and what you need is to parse it to String.
I suggest you use:
DateFormat formatter = new SimpleDateFormat("d/MM/yyyy hh:mm:ss a");
//this will give you the format '8/15/2013 2:00:48 PM'
String d = formatter.format(date);
Hope this helps.
EDIT:
Also seams you want to have your date instance in -07:00 timezone
So you can change your line
DateTime dateTime = formatter.parseDateTime(input);
for
DateTime dateTime = formatter.withZone(DateTimeZone.forID("-07:00")).parseDateTime(input);
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.
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.