Problem in changing String to date in blackberry? - blackberry

i am using following code but the long values are not same any one can help me .
{
long longCurrentTime=System.currentTimeMillis();
System.out.println("Current time is..."+longCurrentTime);
Date date=new Date(longCurrentTime);
SimpleDateFormat dformat=new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss");
String inStringTime=dformat.format(date);
long byStringLongValue=HttpDateParser.parse(inStringTime);
System.out.println("String to long conversion..."+byStringLongValue);
}
in this code both long values are coming different.
thanks

The reason is that your date format ignores the millisecond part.
Add the milliseconds part and verify the result.
SimpleDateFormat dformat=new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss S");
^^^^

Related

How to convert date of format dd MMM, yyyy into dd/MM/yyyy in mvc

I want to convert date into above mentioned format. I have used:
DateTime date1 = DateTime.ParseExact(date, "dd/MM/yyyy", null);
But it gives exception as string is not recognized as a valid date time.
Note:date is of a string datatype and it is in dd MMM, yyyy format.
string is not recognized as a valid date time
Because you're trying to parse the date string from this format:
"dd/MM/yyyy"
But, as you state, the date string is in this format:
"dd MMM, yyyy"
ParseExact means just that... exact. Parse the date from the format it's in:
DateTime date1 = DateTime.ParseExact(date, "dd MMM, yyyy", null);
Then you can output that value in any format you like:
date1.ToString("dd/MM/yyyy");
ParseExact takes the source format as second parameter. Try to use DateTime.ParseExact(date, "dd MMM, yyyy", null);.
#David beat me to the answer, but I just want to add that you should use TryParseExact rather than ParseExact. That way, you can recover from potential problems. For example:
if (DateTime.TryParseExact(date, "dd MMM, yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime date2))
{
date2.ToString("dd/MM/yyyy");
}
else
{
// handle date in incorrect format
}
Can yo please try this:
string dateString = "15 Jun, 2017";
DateTime result = DateTime.ParseExact(dateString, "dd MMM, yyyy", null);
// Changing to dd/MM/yyyy
string myDate = result.ToString("dd/MM/yyyy");

Converting String to NSDate Giving Wrong Date

I have a String that I converted using stringFromDate and now I'm trying to convert it back, however when the UIPicker starts, it's giving me the wrong day
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd MMM YYYY"
print(birthday) // logs 15 Jan 1992
let date = dateFormatter.dateFromString(birthday)
self.datePicker.setDate(date!, animated: true)
I tried hardcoding "15 Feb 1992" but still the same result. The date on UIDatePicker shows 22 Dec 1991 on Start.
If I use hardcore 10 Jan 1980, it starts from 23 December 1979.
(I don't know if that's the case but I have MMM dd YYYY in UIPickerView whereas it's dd MMM YYYY for the strings.. I don't think though because while saving, it saves the right value)..
To use correct format string is most important..
YYYY is week-based calendar year. (used in ISO week-year calendar)
yyyy is ordinary calendar year.
so, You should use 'yyyy' instead of 'YYYY'.
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd MMM yyyy"
print(birthday)
let date = dateFormatter.dateFromString(birthday)
self.datePicker.setDate(date!, animated: true)
For more string format for Date: refer this link
https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx
You are using the wrong format. You need dd MMM yyyy.

Issues in converting date string (any time zone) to NSDate of French Time Zone

I want to convert a date string (can be in any time zone) to a date in French Time Zone. I am using following code.
NSString * dateString = #"27/05/2015 - 19:00" // system time zone is GMT +5
NSDateFormatter* frenchDateFormatter = [[NSDateFormatter alloc] init];
[frenchDateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"Europe/Paris"]];
[frenchDateFormatter setDateFormat:#"dd/MM/yyyy - HH:mm"];
NSDate *frenchDate = [frenchDateFormatter dateFromString:dateString];
NSLog(#"%#",frenchDate);
NSString * frenchString = [frenchDateFormatter stringFromDate:frenchDate];`
Elaboration
--> System time zone is GMT +5
--> French time zone is GMT +2
Date string = 27/05/2015 - 19:00
Expected result = 27/05/2015 - 16:00
Actual result (NSDate) = 2015-05-27 17:00:00 +0000
Actual result (NSString from date) = 27/05/2015 - 19:00
Kindly point out if I am missing something
If you use NSLog to display dates it'll be displayed in UTC. So either you have to convert in your head, or don't use it. I wrote a long answer explaining this to a different question.
Because you have set the timezone of your parsing dateFormatter to Paris the string you parse is treated as "time in paris". That's your problem, you actually wanted to parse it in local time.
The results you get are exactly as one would expect.
You create a NSDate that relates to "19:00 in Paris". Since Paris is UTC+2 that date is 17:00 in UTC (or in +0000). If you convert that date back to "time in Paris" you end up with the same string as before.
If you want to convert the representation of a point in time in your location to a different representation at a different location you have to use two dateFormatters.
NSString *localDateString = #"27/05/2015 - 19:00" // system time zone is GMT +5
NSDateFormatter* localDateFormatter = [[NSDateFormatter alloc] init];
[localDateFormatter setTimeZone:[NSTimeZone localTimeZone]];
[localDateFormatter setDateFormat:#"dd/MM/yyyy - HH:mm"];
NSDate *date = [localDateFormatter dateFromString:localDateString]; // date contains point in time. It no longer has a timezone
NSDateFormatter* franceDateFormatter = [[NSDateFormatter alloc] init];
[franceDateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"Europe/Paris"]];
[franceDateFormatter setDateFormat:#"dd/MM/yyyy - HH:mm"];
NSString * timeInFranceString = [franceDateFormatter stringFromDate:date]; // representation of the point in time from above for people in Paris
This line prints out the date/time in GMT, as it calls [NSDate description], and there is a potential difference between systemTimeZone and GMT, hence the difference you are seeing:
NSLog(#"%#",currentDate);
If you want to see what the date/time is for a particular timezone then use the NSDateFormatter object to get the string.
A date doesn't have a time zone information. A date is internally represented as a number. We don't have to know anything about that number (it's a number of seconds from a fixed date in UTC), the important thing is to understand that to display a date to a user, you have to convert it to a string first.
A string representation of a number is generated from a date using a date format and a time zone. For all date -> string and string -> date conversions you can use NSDateFormatter.
You have successfully parsed currentDate from your string representation. If you want to reverse the process and get the string representation, just use [currentDateFormatter stringFromDate:currentDate]
Check at http://www.timeanddate.com/worldclock/
Right now Paris is two hours ahead of UTC. The result is absolutely correct. NSDate keeps dates in UTC. The idea is that if any two people look at their watch at the same moment, and convert the time they see on their watch to NSDate, they will get the same result.
You cannot get an NSDate for a timezone. NSDate doesn't support time zones. The only way to get a date with a time zone is to use NSDateFormatter to convert it to a string.

Converting NSString into NSDAte once again

i have a label which is :
_labelCell.text = [2014-06-22 20:27:48 +0000];
What i want to do is to convert this string into NSDate so i can format it into something like : EEEE dd MM yyyy
i try :
// convert to date
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd'T'HH:mm:ss'+0000'"];
NSDate *dte = [dateFormat dateFromString:str];
NSLog(#"Date: %#", dte);
but it always give me a NULL NSDate
Can someone help me on this little thing ?
Thank you very much.
Your date format needs to resemble the format of the date. See http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns for the format patterns. For your date 2014-06-22 20:27:48 +0000 you need to use "yyyy-MM-dd HH:mm:ss ZZZ". Note that it must be "yyyy", not "YYYY", and the zone field should be parsed rather than treated as a literal. There is no "T" separating date and time.
Your date formatter is expecting a T in between the date and time. It returns null because there the string has a space instead of a T.
You're also missing a space before the timezone.
Fix those two issues, and it should work:
dateFormat.dateFormat = #"YYYY-MM-dd HH:mm:ss '+0000'";
Beware this might give you the wrong date, because of time zone issues. Test that out, and if it doesn't work adjust accordingly with dateFormat.timeZone = ...

Zoned dateTime to UTC Time - LocalDateTimePattern throws exception

I am trying to obtain the UTC time from a zoned datetime using LocalDateTime pattern in NodaTime using the below code.
public string getUtcTimeFromZonedTime(string dateTimeString, string timeZoneID,
string dateTimePattern, bool isDateTime)
{
if (string.IsNullOrEmpty(dateTimePattern))
{
if (isDateTime)
{
dateTimePattern = "M/dd/yyyy HH:mm:ss tt";
}
else
{
dateTimePattern = "M/dd/yyyy";
}
}
var pattern = LocalDateTimePattern.CreateWithInvariantCulture(dateTimePattern);
var parseResult = pattern.Parse(dateTimeString);
if (!parseResult.Success)
{
// throw an exception or whatever you want to do
}
var localDateTime = parseResult.Value;
var timeZone = DateTimeZoneProviders.Tzdb[timeZoneID];
// TODO: Consider how you want to handle ambiguous or "skipped" local date/time
// values. For example, you might want InZoneStrictly, or provide your own custom
// handler to InZone.
var zonedDateTime = localDateTime.InZoneLeniently(timeZone);
return zonedDateTime.ToDateTimeUtc().ToString();
}
I get an exception during Parsing during below mentioned scenarios-
1) If pattern is like "MM/dd/yyyy HH:mm:ss tt" and the DateTime string is something like "5/28/2013 1:02:ss PM"
2) If pattern is like "MM-dd-yyyy HH:mm:ss tt" and the DateTime string is something like "5/28/2013 1:02:ss PM"
For the first case, it will work if I change my pattern to "M/dd/yyyy HH:mm:ss tt", but i will end up losing the leading zero. Second case will work if I change the pattern to "MM/dd/yyyy HH:mm:ss tt"
Is there any alternative way for getting the UTC values or am I doing something wrong over here.
1) If pattern is like "MM/dd/yyyy HH:mm:ss tt" and the DateTime string is something like "5/28/2013 1:02:ss PM"
Yes, because you've specified that you'll give it a two-digit hour, and you've only given one digit. Note that if you're using an AM/PM designator, you probably want h rather than H anyway.
2) If pattern is like "MM-dd-yyyy HH:mm:ss tt" and the DateTime string is something like "5/28/2013 1:02:ss PM"
Yes, because you've specified that you want - as the separator, but you're using / in the text.
I suspect you want:
dateTimePattern = "M/dd/yyyy h:mm:ss tt";
Note that this has nothing to do with converting to UTC - it's just the parsing to LocalDateTime that's causing you problems.

Resources