Qt Qtime am to pm and vice versa - qt

I want to change the "AM" to "PM" string and vice versa in Qt.
I created a timeEdit in .ui and I set the displayFormat to HH::mm a but I always get AM even we're in afternoon now. how to change the code to get a display either "AM" or "PM".
I know that there's a function member named amText() and pmText(), but they return a string.
does someone have any idea?

The correct format string to get AM or PM depending on the actual time is "ap". Thus, change your format string to "HH:mm ap".

Related

Json object: is putting a date as string a bad practice?

I have the following json object:
public class User {
private String name;
#JsonFormat(pattern="dd-MM-yyyy")
private Date birthdate;
}
but I'm thinking of changing it to:
private String birthdate; //mmm...now need to make sure the format is respected..
Problem is that this object has to be saved inside of a DB and then retrieved.
Suppose I have an user in the CEST time, while my application's time is UTC, so 2 hours behind.
They send "15-04-1990" (time is implicitely 00:00:00), the system then interprets this date as "14-04-1990"(time is implicitely 10:00:00 PM) due to the 2 hours difference.
When they go retrieve the information again they will see 14-04-1990!
Ok, I could just add the sender's timezone to the request. But I don't like it: take a 3rd party residing on the application's country wanting to check some information (for example an helpdesk operator): they would still see the date in the wrong timezone!
So, do you see any problem in receiving the date as string?
And if that would be fine how to make sure that the string contains a date formatted in "dd-mm-yyyy"?
One of things could be to save the date and time objects as timestamp of GMT and then change every time zone with respect to that.

Qt LineEdit setInputMask() with setText() and QRegExp

I have a QLineEdit for Date in mm/dd/yyyy format. I am getting input using the keyboard and not using QDateEdit because of the requirement. And when the lineEdit comes to view, it has to show to the user the current date. I need the following for the lineEdit.
I need the two slashes always to be displayed and the cursor has to skip while entering or deleting.
I should not allow the user to enter an invalid date i.e while entering itself the lineEdit should not get invalid numbers.
I have to set the current date as the default text when the lineEdit comes to view.
For the first point, I tried using setInputMask("99/99/9999") but with this I can't set the current date using setText(). And how to use QRegExp to not to allow lineEdit get an invalid number while employing setInputMask()?
QDateEdit will serve your purpose.
use setDisplayFormat("dd/MM/yyyy").
QDateEdit wont allow invalid dates
You can use QDateEdit::setDate() obtained from
QDateTime::currentDateTime()
For setting text into QLineEdit with setInputMask("99/99/9999") you should format text depending on your mask:
lineEdit.setText("{:02d}/{:02d}/{:04d}".format(m, d, y))
Alternatively, you can temporary disable InputMask, format your date without /, set it and re-enable InputMask. But make sure that number of symbols in every part is correct.
lineEdit.setInputMask("")
lineEdit.setText(date_str.replace("/", ""))
lineEdit.setInputMask("99/99/9999")

I want to display time as 09032014124008 rather than 9/3/2014 12:40:08 AM

I want to display time as 09032014124008 rather than 9/3/2014 12:40:08 AM
When I use DateTime.Now, the format is the latter one.
Kindly suggest how can I get it in the required format.
You can specify a custom format string on the call to .ToString()
For your example you would use:
DateTime.Now.ToString("MMddyyyyhhmmss")
Also see here for additional format string options

Changing the way QDate displays date format

My app has a has a QTreeWidget that takes a QDate in one of its columns. Since the columns accept QVariants they can hold practically any kind of data. I've found that the TreeWidget must use the actual QDate objects instead of QStrings for the column sorting functionality to work correctly. If I use QStrings for dates, they won't sort in the proper chronological order but rather by the numerical values of the strings. (which is wrong!) My program supports several date formats: USA Style, European style, and ISO-8601 style. I would like to keep everything consistent throughout the app depending on which date format the user has chosen.
However, I noticed that QDate only displays dates in MM/DD/YYYY format. There's also a strange bug where the QDate displays MM/DD/YYYY on Windows but the exact same code displays MM/DD/YY on Linux. How can I get the QDate to show dates in YYYY/MM/DD or DD/MM/YYYY format without converting to a QString? It is essential to keep everything in QDate format so I don't break the column sort function in the QTreeWidget.
Here's my code that converts the QString to a QDate: (nextitem is a QStringList)
// Convert QString date to QDate to make it sort correctly
QDate entrydate;
QString id=nextitem.at(2);
id=id.remove("/");
QString datepattern;
switch(Buffer::date_format){
case 0: // European
datepattern="ddMMyyyy";
break;
case 1: // USA Style
datepattern="MMddyyyy";
break;
case 2: // ISO
datepattern="yyyyMMdd";
break;
}
entrydate=QDate::fromString(id,datepattern);
QDate follows the current settings in the operating system. If that setting happens to be MM/DD/YY, then that's what you'll get. Note: this is what the user wants. A uniform date format in all applications.
The setting is also influenced by the current locale settings in the OS. In the USA for example the default would be MM/DD/YY, while in most European countries it's DD/MM/YY.
In other words: don't worry about it. It's not a bug, it's a feature. And you shouldn't try to work around it. Users don't want their applications ignoring their system settings. If my system is set to display dates in MM/DD/YY, I certainly wouldn't want your app doing its own thing and showing me dates in YYYY/MM/DD format instead.
You can use this code:
QVariant var = item->data(i, Qt::DisplayRole);
if (var.type() == QVariant::DateTime)
{
QDateTime dt = var.value<QDateTime>();
QString value = dt.toString("yyyyMMdd"); // Or whatever format you need
}
else
QString value = item->text(i);

How can I make input fields accept locale dependent number formatting?

I'm working on a Spring MVC Project and ran into a problem with the internationalization in forms, especially the number formatting.
I already use fmt:formatNumber to format the numbers according to the current selected locale.
<fmt:formatNumber value="${object[field]}"/>
Like this, number formatting works well when I display numbers. But how about the forms?
At the moment, the input fields that are supposed to receive float values are prefilled with 0.0 and expect me to use "." as decimal separator, no matter what locale is selected. Values containing "," are refused by the server (...can not convert String to required type float...).
How can I make my input fields use and accept the appropriate number format as well?
Did you have a look at #NumberFormat? If you annotate the property the input field is bound to, this should result in the proper formatting. Something like:
#NumberFormat(style = Style.NUMBER)
private BigDecimal something;
This style is the "general-purpose number format for the current locale". I guess, the current locale is determined threadwise from the LocaleContextHolder.
Your app needs to be annotation-driven, also see the section "Annotation-driven Formatting" in the docs.
You might want to take a look at the DecimalFormatSymbols as suggested in this answer.

Resources