hh:mm aa (12 hour format ) convert in HH:mm (24 hour format) - blackberry

I have String time="02:30 PM" means 12 hour format and i want to convert this time in 24 hour format .
I want this o/p: 14:30. so how can i convert this ?

I don't know anything about berries, but in case the API is missing a proper formatting function, you can always get your hands dirty with the string itself:
static String convert(String time){
boolean pm = "PM".equals(time.substring(6).toUpperCase());
int h = Integer.valueOf(time.substring(0,2));
if (h!=12)
h+=pm?12:0;
else
h=pm?h:0;
return ((h<10)?"0":"")+h + ":" + time.substring(3,5);
}

Try this code.
This will give the current time in 24 Hours format.
SimpleDateFormat formate = new SimpleDateFormat("HH:mm");
String newTime = formate.formatLocal(System.currentTimeMillis());

Related

Convert 24 hours to 12 hours in dart

I want method to convert string 24 hours time format to 12 format
For example the method takes string 16:30
Then return string 4:30 pm
The intl package do not work because the package needs the data and time but I'm reading the time from Jason file without date
as an option
String convertTo12(String hhMM) {
final arr = hhMM.split(':');
final h = int.tryParse(arr[0]);
return '${h > 12 ? h % 12 : h}:${arr[1]}';
}
final str = '16:30';
print(convertTo12(str)); // 4:30

Converting 12 digit ticks to Date

I am trying to convert 12 digit c# date time ticks formatted time. (648000000000). With the help of following link I added an extension to my code How to get 18-digit current timestamp in Swift?.
extension Date {
init(ticks: UInt64) {
self.init(timeIntervalSince1970: Double(ticks)/10_000_000 - 62_135_596_800)
}
}
let date = Date(ticks: 648000000000)
When I try to see result date it prints following;
0001-01-03 18:00:00 +0000
However, when I try to convert it hour and minute format output is irrelevant like 19:55
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
dateFormatter.string(from: date)
My first question is how can I format it to get only 18:00. Second is why it is printing 18:00 when I print only date, but 19:55 when I format it?
Just don't subtract 62_135_596_800
extension Date {
init(ticks: UInt64) {
self.init(timeIntervalSince1970: Double(ticks)/10_000_000)
}
}
1970-01-01 18:00:00 +0000
The other problem: When you create date and print it, the string is formatted in UTC time zone (offset GMT+0). But DateFormatter returns string representation dependent on its time zone, which is the local timezone by default.
You can fix your code just by setting dateFormatter's timeZone to UTC
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
18:00
Apparently your timestamp represents a duration as the number of 100-nanosecond ticks, not a date. If you divide the number by 10^7 then you get the number of seconds. These can be printed as a duration with a DateComponentsFormatter.
Example:
let ticks = 648000000000
let seconds = TimeInterval(ticks) / 10_000_000
let fmt = DateComponentsFormatter()
fmt.allowedUnits = [.hour, .minute]
print(fmt.string(from: seconds)!)
18:00
The duration is 64800 = 18 * 60 * 60 seconds, that are exactly 18 hours.

How do I create a DateTime instance in a specific timezone?

I am calling a legacy service that returns all times in a string format assuming that the timezone is EST. How do I create a DateTime instance and specify that the timezone is EST? When I display the times to the user's I will translate to their device timezone.
I wrote a package for this. It's called Instant, and it can fetch the latest DateTime in any given zone worldwide. Take a detailed look at https://aditya-kishore.gitbook.io/instant/
The basic usage to get a DateTime in a timezone is fairly simple.
Note: I assume below that your legacy service delivers a String with format HH:MM in 24 hr time, and that it doesn't deliver a date. If any of the following is untrue, lemme know and I'll change the code sample.
List<int> _hourmin (String parse) {
List<int> timeinint = [];
if (parse.length == 5) {
//if parse is something like 10:00
timeinint.add(int.parse(parse.substring(0,2))); //Add hours (10)
timeinint.add(int.parse(parse.substring(3,5))); //Add minutes (0)
} //hours >= 10
if (parse.length == 4) {
//if parse is something like 4:06
timeinint.add(int.parse(parse.substring(0,1))); //Add hours (4)
timeinint.add(int.parse(parse.substring(2,4))); //Add minutes (6)
} //hours < 10
return timeinint;
}
DateTime utcDateTimeFromZone(
{#required String zone,
int year = 0,
int month = 0,
int day = 0,
int hour = 0,
int minute = 0,
int second = 0,
int millisecond = 0,
int microsecond = 0}) {
hour = (hour - timeZoneOffsets[zone].truncate()) % 24; //Convert to UTC from timezone
if (hour<0) {
hour = 24 + hour;
}
minute = (minute - ((timeZoneOffsets[zone]%1)*60).round()) % 60;
if (minute<0) {
minute = 60 + minute;
}
//Convert to UTC from timezone
DateTime inUTC = DateTime.utc(year, month, day, hour, minute, second, millisecond, microsecond); //Initialize in UTC time
return inUTC;
}
//We get an array containing the hours minutes
List<int> time = _hourmin("4:25"); //put your API call as the param here
//This is what you need
DateTime finished = utcDateTimeFromZone(zone: "EST", hour: time[0], minute: time[1]);
print(formatTime(time: finished.toLocal())); //1:25 for me, because I'm PST, but this works with any local timezone
Hope that clears it up!
The included DateTime class only supports the local timezone or UTC but no specific timezone.
https://pub.dartlang.org/packages/timezone would provide that but it looks like it needs an update for Dart 2.0.0
https://pub.dartlang.org/packages/time_machine is a newer one.

how to make 24 hours time format from string in Swift

I want to convert a string that was generated by the user to a Date data type. I want the time to be in 24-hour format
let dateFormatter = DateFormatter()
let timeAsString : String = "22:30"
dateFormatter.dateFormat = "HH:mm"
let timeFromString = dateFormatter.date(from: timeAsString)
result : "Jan 1, 2000 at 10:30 PM"
but the result is in 12-hour format. How can I get 22:30 as a Date data type?
Date has no format, so only can change the string converted from the date
Swift 4
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
let newDateString = dateFormatter.string(from: yourDate)
for different date format, you can check nsdateformatter.com
You code is absolutely correct, there no problem in your code. String HH in date format represent 24 hours time display format.
But developer (application) has no control over time format. You can set date format string supporting 24 hours time but if user has (not enabled) turned of 24 hours support from device then it will display time for 12 hours format.
Check your simulator/mac system/iPhone device time format and set it for 24 hours display.
Refer this apple document for 24 hours time support: Date Formatters
The representation of the time may be 13:00. In iOS, however, if the user has switched 24-Hour Time to Off, the time may be 1:00 pm.
func convertToString(of dateTo: Date) -> String {
let dateFormatter = CustomDateFormatter()
//Your New Date format as per requirement change it own
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let newDate: String = dateFormatter.string(from: dateTo) //pass Date here
// print(newDate) //New formatted Date string
return newDate
}

Convert date into julian date

I want to convert todays date into the julian date format . Suppose if the date is 31/12/2011(31st dec 2011) , then the julian date should be 11365(yyddd) .
Any help would be much appreciated
Thanks
One simple way to get the date in yyddd format would be doing some mathematics:
date theDate = 31\12\2011;
int theOrdDate;
;
theOrdDate = 1000 * (year(theDate) mod 100) + dayOfYr(theDate);
// or
theOrdDate = 1000 * (year(theDate) - 2000) + dayOfYr(theDate);
EDIT: one possible drawback: this calculation will return only 4 digits if the year is less than 2010.
In AX2012 this task is super easy.
str myJulian;
myJulian = date2Julian(today());

Resources