Format currency with dot instead of comma using i18n - spring

We are using java.text.NumberFormat class to format the currency values using the method getInstance(Locale paramLocale). Our issue is when we pass es_CO(Columbia) language code it automatically formats it in value 123,00 instead of 123.00. Is there a way to format with dot instead of comma?
I am using Spring platform(hybris)
Please note due to business reasons it is not possible for me to change the locale.

You can use DecimalFormat to have your own format.
Look at this How can I format a String number to have commas and round?

Related

How to use Nifi expression language to change a date into a folder path?

In nifi, I need to transfer a bunch of json files to HDFS. The json files have a field called "creationDate" which has the date in UNIX format. I need to use the date in there to funnel the file to HDFS directories that are named after dates, like "2019-01-19" "2019-01-20" "2019-01-21" etc.
At first I used an "EvaluateJsonPath" processor going to a "PutHDFS" processor. The "Evaluate..." processor had "creationDate" as the property and "${creationDate} as the value. In the PutHDFS processor, for directory I put "/${creationDate}"
But then I realized that the date in the json file has the full timestamp, like "2019-01-19T04:34:28.527722+00:00
Obviously I don't need all that, just the first eight digits. So how can I turn this big string into a neat 8-digit directory name? Will I need to use a regex, and if so, how can this be implemented? Thanks in advance for any help.
You can use UpdateAttribute and use the date expression language functions to format it.
https://nifi.apache.org/docs/nifi-docs/html/expression-language-guide.html
Example (not specific to your format):
${creationDate:toDate('MM-dd-yyyy'):format('yyyy/MM/dd')}
In UpdateAttribute you would add a new property name creationDate and set the value to an expression like above.

Spring localization truncates decimals

I'm using Spring localization with a properties file and basically want to localize {0}€ with the parameter being a BigDecimal. The localzied output will show 1€ instead of 1.00€ while 1.01€ will be displayed as should be.
Is there a way to leave blank decimal places untouched?
You can define message format for numbers this way
product.price = Price: {0, number, #.##}€
See Dealing with Compound Messages and MessageFormat

Figure date format from string in ruby

I am working in a simple data loader for text files and would like to add a feature for correctly loading dates into the tables. The problem I have is that I do not know the date format before hand, and it will not be my script doing the inserts - it has to generate insert statements for later use.
The Date.parse is almost what I'd need. If there was a way to grab the format it identified on the string in a way I could use to generate a to_date(...)(Oracle standard) would be perfect.
An example:
My input file:
user_name;birth_date
Sue;20130427
Amy;31/4/1984
Should generate:
insert into my_table values ('Sue', to_date('20130427','yyyymmdd'));
insert into my_table values ('Amy', to_date('31/4/1984','dd/mm/yyyy'));
Note that it is important the original string remains unchanged - so I cannot parse it to a standard format used in the inserts (it is a requirement).
At the moment I am just testing a bunch of regexes and doing some validation, but I was wondering if there was a more robust way.
Suppose (using for example String#scan), you extracted an array of the date strings from a single file. It may be like:
strings = ["20130427", "20130102", ...]
Prepare in advance an array of all formats you can think of. It may be like:
Formats = ["%Y%m%d", "%y%m%d", "%y/%m/%d", "%m/%d/%y", "%d/%m/%y", ...]
Then check all formats that can parse all of the strings:
require "date"
formats =
Formats.select{|format| strings.all?{|s| Date.strptime(s, format) rescue nil}}
If this array formats includes exactly one element, then that means the strings were unambiguously parsed with that format. Using that format, you can go back to the strings and parse them with that format.
Otherwise, either you failed to provide the appropriate format within Formats, or the strings remained ambiguous.
I would use the Chronic gem. It will extract dates in most formats.
It has options to resolve the ambiguity in the xx/xx/xxxx format, but you'd have to specify which to prefer when either match.

Parameterizing a freemarker builtin

Can the builtin part of a expression in freemarker be parameterized?
If so, how?
For example, a date can be formatted with the following builtins:
${openingTime?string.short}
${openingTime?string.medium}
${openingTime?string.long}
${openingTime?string.full}
Can the string.short/string.medium/... builtin names be parameterized too?
For example, I'd like to do something like:
${openingTime?${mydatefmt}}
where mydatefmt is 'string.short' or any valid format.
That would make it easily possible to change the date formats on a freemarker page.
I want to limit the change to a page/file and not apply globally.
Does something like this need to be put into a Freemarker macro that might anticipate all possible types of date formats that might be needed?
One approach is using openingTime?string(pattern), where pattern can be any kind of expression that evaluates to a string. But it's quite verbose and somewhat slow, as the patter will be re-parsed again and again.
Another approach is setting the date_format, time_format and datetime_format FreeMarker settings, and then just write ${openingTime}. (Actually, if openingTime isn't a javax.sql subclass of java.util.Date, you have to write ${openingTime?datetime}, because the Java API doesn't know the difference date-time, time and date-only, but this is another story.) FreeMarker settings can be set globally (better said, on Configuration-level), on template-level (but you don't do that usually), or on the Environment. See http://freemarker.org/docs/pgui_config_settings.html. The last can be done in FTL too, like with <#setting datetime_format = 'yyyy-MM-dd HH:mm:ss zzzz'>.
You have to specify the formatting pattern explicitly : ${openingTime?string("yyyy-MM-dd HH:mm:ss zzzz")}
The date format can be a expression, for example : ${openingTime?string(mydatefmt)}.

Rails url constraint

Im making a url pass in this date format at the very end of the string
2011-11-02T13:59:26.13Z
I can do this
a.sub!(/\..*/,'')
to knock off the .(digit)(digit)Z and work with the time in my controller.
if i put the time in regular format in my url, it works fine. If i put in the specified format above, i get a blank page. If i add the constraint i made (works fine with chopping the end off in the console, I get an routing (no route matches [GET] ...). What should I do to allow to pass in the date format i need (Im using rails 3 if thats important)
The colon is a reseved character and cannot be used within a URL. See RFC 2366 section 2.2
Also, the . (dot) is used to designate a format type in rails, so when this is passed into the router it will try to render a layout for the '13Z' format.
You should encode that time in another format that does not require these characters.
E.g.
20111102135926
Because each field is fixed width it is still easy to parse with a regex.
You could also atime which can be parsed directly in Ruby.

Resources