Transform a string into ISO datetime format | Freemarker - freemarker

So I get the date and time which is, for example, 17.11.2021 and 12:44. Now I want to convert this date and time into the formate which Freemarker is using (yyyy-MM-dd hh:dd:mm:ss). However the problem here is that I can't convert it properly. I tried like:
${myDateTime?datetime.iso?string("yyyy-MM-dd HH:mm:ss")}
But this wont work. I always get an error message. Can anyone explain or show me the correct formation in this case ?

Assuming myDateTime is a String (and not already a Date object), and it looks like 17.11.2021 12:44, you can convert it to ISO format like this:
${myDateTime?datetime("dd.MM.yyyy HH:mm")?string.iso}
Above myDateTime?datetime("dd.MM.yyyy HH:mm") converts the string to a Date (which is a format-independent representation), and then ?string.iso formats the Date with ISO format.
Note that it would be much cleaner if myDateTime was a Date, not a String. Then you just do this: ${myDateTime?string.iso}

Related

Format currency with dot instead of comma using i18n

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?

How to convert current date format(YYYY-MM-DD HH:MM:SS) to (YYMMDD) format in freemarker language

I have used code ${.now?iso("UTC")} to get the current date and time stamp inside the freemarker templates, but I would like to convert the current date format (YYYY-MM-DD HH:MM:SS) to (YYMMDD) format. Please provide your suggestion.
At the moment (2.3.28) ?string doesn't support forcing UTC. So if the time_zone FreeMarker configuration setting is not UTC, you can do this:
<#setting time_zone="UTC">
${.now?string('yyyyMMdd')}
However, then the further dates will also use UTC. If that's a problem then either you use a custom date format (which is a date format implemented in custom Java code, so it can do anything; see: https://freemarker.apache.org/docs/pgui_config_custom_formats.html#pgui_config_custom_formats_ex_cust_algo_date), or use this hack:
${.now?date?iso('UTC')?replace('-', '')}

Need to custom format a ruby date object?

how do I format a ruby date as the following?
sep14.2016
It looks like the format is 3 characters of month followed by 2 characters of day with a period and a year.
Try This
Date.new(2016,9,14).strftime('%b%d.%Y')
d = Date.parse('14 September 2016')
to get your date object.
d.strftime('%b%d.%Y') #=> Sep14.2016
This will format your date like you wanted. Keep in mind d is not modified, so it is still a Date object. You need to assign the result of the strftime method to any variable.
You can use the downcase method to remove any capitalization.

How to parse a date from string (template needed)

I have a string like 2015-07-09T12:00:00+00:00. How can I parse it using Date.strptime()? Help me to build a template.
Try using DateTime.strptime instead of Date.strptime, since you're using time and timezone.

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.

Resources