Date format similar to NSDateFormatterMediumStyle for Monotouch? - iphone

I am trying to format a date as it appears in iOS calendar application.
The format is the NSDateFormatterMediumStyle and displays a date such as "Nov 27, 2011”.
Documentation
However such a format is not available in the .NET Framework date formats, so I have to use the NSDateFormatter to format the date.
Documentation
The problem I have is that in Monotouch I cannot find a method in NSDateFormatter to format the date.
In the Apple examples there is a [dateFormatter stringFromDate:date]; method, however it looks that this method is missing in Monotouch or there is an alternative?

Jason's comment makes a great point about using the Rosetta. It shows that the stringFromDate:date selector is binder inside the NSDateFormatter type.
note: since a lot of the base (shared between MonoTouch and MonoMac) code is open source you can also find the binding sources on github.
From the above I assume you should be able to get the results you're looking for (i.e. iOS, not .NET, culture-aware date/time strings) by doing:
var f = new NSDateFormatter ();
f.DateStyle = NSDateFormatterStyle.Medium;
string result = f.ToString (nsdate);

.NET supports custom date formats. This should do what you want:
myDate.ToString("MMM d, yyy");

Related

GWT compiling java to javascript and time format

in Java I set the date as time in mSec since 1970, e.g. futuredate=1640995200000l //1 Jan 2022.
When this is compiled into JavaScript by GWT, I believe it uses the Jsdate library which says it is a native javascript date object
1 jan 2022 ends up as this object in the JavaScript _.futuredate={l:3120128, m:391243, h:0}
Can someone help me interpret this format please, it doesn't quite make sense to me
thanks
To work with date and time on the GWT client side one can use com.google.gwt.i18n.shared.DateTimeFormat. com.google.gwt.i18n.shared.DateTimeFormat.PredefinedFormat includes already many predefined formats, but you can naturally use your own, such as "EEEE, y MMMM dd".
Based on your example:
Date myDate = DateTimeFormat.getFormat("dd.MM.yyyy").parse("01.01.2022");
DateTimeFormat myFormat = DateTimeFormat.getFormat(PredefinedFormat.ISO_8601);
String s = myFormat.format(myDate);
The first line only simulates the date that you already set in Java and it is meant to show another example of a date format and how to parse a String.

How to get the Date with offset taken into account from XmlGregorianCalendar?

i have an XMLGregorianCalendar in this form: 2019-06-24T18:18:55.007+02:00
How can i get the right date (with offset taken into account) in String like this : 24/06/2019 16:18 ?
You tagged your question java-6. Yet I am presenting the modern answer using java.time, the modern Java date and time API. This is available in Java 6 through ThreeTen Backport (see the link at the bottom).
Depending on from where you got your XMLGregorianCalendar you may not need to have one. If I understood correctly, this class was used for representing dates and times in XML documents, where they are formatted like for example 2019-06-24T18:18:55.007+02:00. This format is close enough to ISO 8601 that the classes of java.time can parse it directly, which we prefer:
Locale userLocale = Locale.forLanguageTag("mt-MT"); // Maltese Malta
DateTimeFormatter displayFormatter
= DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)
.withLocale(userLocale);
String fromXml = "2019-06-24T18:18:55.007+02:00";
OffsetDateTime dateTime = OffsetDateTime.parse(fromXml);
String formattedUtcDateTime = dateTime.withOffsetSameInstant(ZoneOffset.UTC)
.format(displayFormatter);
System.out.println(formattedUtcDateTime);
Output from this snippet is (tested in Java 1.7.0_67 with ThreeTen Backport version 1.3.6):
24/06/2019 16:18
Assuming that the formatting is for displaying to a user, I recommend using Java’s localized formats. I have chosen Maltese locale because it matches your requested format exactly. If your user is in a different locale, you should still consider the format of that locale.
Converting from XMLGregorianCalendar
If you got your XMLGregorianCalendar from a legacy API that you cannot afford to change just now, there are a number of options for converting it.
One is to get the string back from the XMLGregorianCalendar:
XMLGregorianCalendar xmlgc = DatatypeFactory.newInstance()
.newXMLGregorianCalendar("2019-06-24T18:18:55.007+02:00");
String fromXml = xmlgc.toString();
Now the rest is as above. Pro: it’s short, and I don’t think there are any surprises in it. Con: To me formatting back into the original string and parsing it once more feels like the detour.
Another option goes through the outdated GregorianCalendar class:
GregorianCalendar gc = xmlgc.toGregorianCalendar();
ZonedDateTime zdt = DateTimeUtils.toZonedDateTime(gc);
String formattedUtcDateTime = zdt.toOffsetDateTime()
.withOffsetSameInstant(ZoneOffset.UTC)
.format(displayFormatter);
Pro: I think it’s the official conversion. Con: it’s a line longer, and as I said, it uses the poorly designed and long outdated GregorianCalendar class.
Links
Oracle tutorial: Date Time explaining how to use java.time.
Java Specification Request (JSR) 310, where java.time was first described.
ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
ThreeTenABP, Android edition of ThreeTen Backport
Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
Wikipedia article: ISO 8601
The working solution i have found:
private String getFormattedDate(final XMLGregorianCalendar xmlGregorianCalendar) {
Date time = xmlGregorianCalendar.toGregorianCalendar().getTime();
Date rightDate = DateUtils.addMinutes(time, - xmlGregorianCalendar.getTimezone());
String formatedDate = DateHelper.format(DateHelper.YYYYMMDDHHMMSS_DASH_COLONS, rightDate);
return formatedDate;}

Format date and add month to it

I'm currently working with embarcadero c++, this is the first time I'm working with it so it's completely new to me.
What I'm trying to achieve is to get the current date, make sure the date has the "dd/MM/yyyy" format. When I'm sure this is the case I want to add a month to the current date.
So let's say the current date is 08/18/2016 this has to be changed to 18/08/2016 and then the end result should be 18/09/2016.
I've found that there is a method for this in embarcardero however I'm not sure how to use this.
currently I've only been able to get the current date like this.
TDateTime currentDate = Date();
I hope someone will be able to help me out here.
I figured it out.
After I've searched some more I found the way to use the IncMonth method on this page.
The example given my problem is as follows:
void __fastcall TForm1::edtMonthsExit(TObject *Sender)
{
TDateTime StartDate = edtStartDate->Text;
int Months = edtMonths->Text.ToInt();
TDateTime NextPeriod = IncMonth(StartDate, Months);
edtNextPeriod->Text = NextPeriod;
}
After looking at I changed my code accordingly to this
TDateTime CurrentDate = Date();
TDateTime EndDate = IncMonth(CurrentDate, 1);
A date object doesn't have a format like "dd/MM/yyyy". A date object is internally simply represented as a number (or possibly some other form of representation that really isn't your problem or responsibility).
So you don't have to check if it's in this format because no date objects will ever be in this format, they simply don't have a format.
You will have to do additions/subtractions on the Date object that the language or library gives you, THEN (optionally) you can format it to a human-readable string so it looks like 18/08/2016 or 18th of August 2016 or whatever other readable format that you choose.
It might be that the TRANSFER of a date between 2 systems is in a similar format, but then formatting the date like that is entirely up to you.
As for how to do that, the link you posted seems like a possible way (or alternatively http://docwiki.embarcadero.com/Libraries/Berlin/en/System.SysUtils.IncMonth), I'm afraid I can't give you an example as I'm not familiar with the tool/language involved, I'm just speaking generically about Date manipulations and they should ALWAYS be on the raw object.

Why does parsing a date on some devices throw an ArrayIndexOutOfBoundsException?

I have the following string, representing a date and time, that's ISO 8601 compliant:
2014-03-11T11:57:15+0000
I'm using the Codename One SimpleDateFormat class to parse that string in to a Date object and then to populate a Calendar object:
Calendar calendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat(DateTimeFormats.ISO_8601);
Date date = format.parse("2014-03-11T11:57:15+0000");
calendar.setTime(date);
DateFormats.ISO_8601 is defined as
yyyy-MM-dd'T'HH:mm:ssZ
On the Codename One simulator this code correctly parses the string and sets date with the correct values. This also works on a Nokia C1-01. However, running the same code on a Nokia 206 or the DefaultCldcPhone1 emulator from the Java ME SDK results in an ArrayIndexOutOfBounds exception on the call to format.parse. The stack trace beyond that is obfuscated:
java.lang.ArrayIndexOutOfBoundsException
- java.util.Calendar.get(), bci=98
- al.a(), bci=3
- al.a(), bci=18
- al.parse(), bci=1013
Does anyone know why this is happening, how I can prevent it, or suggest a work-around for populating a Calendar from a string without having to write my own parser?
Any help much appreciated!
We recommend using the com.codename1.l10n.SimpleDateFormat class instead of the one from the java.text package. This will allow you to reproduce the issue on the simulator and debug it more accurately.

Grails: how to parse date.toString() without making a custom formatter?

I have a Grails application that needs to parse Dates out of strings that were created with the date.toString() method.
My system's default date.toString() format is "Thu Apr 20 00:27:00 CEST 2006" so I know I can turn a Date into a string and then back into an object using Date.parse('EEE MMM dd HH:mm:ss z yyyy', new Date().toString()).
But that's lame! I shouldn't have to reverse engineer the system's default date format. Plus, I'm not sure under what circumstances the default date format can change, thus breaking that code.
Is there a way to parse a date.toString() back into a Date without using a hand-rolled formatter like that?
Thanks!
Update: I filed this Jira ticket to get such a feature added Groovy. Someone commented on the ticket that Java's date.toString() method is hard-coded to use EEE MMM dd HH:mm:ss z yyyy. That sucks for Java to be so inflexible, but it makes it easier for me to live with hard-coding the formatter!
There's a page over here showing how bad this is in Java (and hence in Groovy)
I agree that there should be a Date.parse method in Groovy which uses this default Date.toString() format.
Maybe it's worth adding a request for improvement over on the Groovy JIRA?
As a temporary workaround, you could add your parse method to the String metaClass in Bootstrap.groovy?
String.metaClass.parseToStringDate = { Date.parse( 'EEE MMM dd HH:mm:ss z yyyy', delegate ) }
Then you can do:
new Date().toString().parseToStringDate()
anywhere in the groovy portions of your grails app
I haven't worked with Grails and I know this is not the answer to your question, but as a workaround, couldn't you just save the format-string as a global variable?
if u use it to convert to json, maybe code below could help:
//bootstrap
JSON.registerObjectMarshaller(Date) {
return it?.format("yyyy-MM-dd HH:mm:ss")
}