Groovy - Converting a date string to a formatted date - date

OK, I am trying to convert a date string from a format like:
2014-01-21 00:00:00
to
01/21/2014
I have tried many variations and am crashing and burning. The issue is that to test I have to create the script, export it in a process in Bonita (a BPM software), Import it and then create some cases. This all takes a long time.
Hopefully someone knows how to do this.
Also, is there a simple groovy editor out there? That would help me learn how to write groovy very quickly.

Groovy Dates have methods parse and format for converting to and from strings in various formats:
def format1 = '2014-01-21 00:00:00'
def format2 = Date.parse("yyyy-MM-dd hh:mm:ss", format1).format("dd/MM/yyyy")
assert format2 == '01/21/2014'
The format of the format strings are the same as Java's SimpleDateFormat.

String olddate='2014/01/21 00:00:00'
Date date = Date.parse("yyyy/MM/dd HH:mm:ss",olddate)
String newDate = date.format( 'MM/dd/yyyy' )
log.info newDate

Related

talend format yyyy-MM-dd'T'HH:mm:ss.SSSz to yyyy-mm-dd HH:mm:ss

I am trying to change the date format in txmlmap component but its not working
i want change date format
from yyyy-MM-dd'T'HH:mm:ss.SSSz to yyyy-mm-dd HH:mm:ss
expected output:- yyyy-mm-dd HH:mm:ss
You can parse your string to a date using your source pattern and then format that date to a string using your target pattern:
TalendDate.formatDate("yyyy-mm-dd HH:mm:ss", TalendDate.parseDate("yyyy-MM-dd'T'HH:mm:ss.SSSz", myDateString))
In almost all coding languages format is text, while date is a double. That means you must first make a date of the first expression, before setting the new format of that date. But in Your case the 'T' is some kind of special format that need to be replaced with a blanck space. I have no idea about what it would look like in talend but in VB it would look like this:
' from yyyy-MM-dd'T'HH:mm:ss.SSSz to yyyy-mm-dd HH:mm:ss
DateTxt = "2022-12-01'T'22:45:10"
DateTxt = Replace(DateTxt, "'T'", " ")
MyDate = CDate(DateTxt)
MsgBox Format(MyDate, "yyyy-mm-dd HH:mm:ss")

I want to compare from date and to date with date order using search orm in odoo

I want to compare from_date and to_date with date_order using search orm in odoo.
I just want to extract date alone because in date_order it is given date with date time. how to work with it ?
here is my code :
from_date = fields.Date(string="From", default="Today")
to_date = fields.Date(string="To")
def update_commission(self):
sale_br = self.env['sale.order']
sale_sr = sale_br.search([('date_order', '=', self.from_date)])
Modify your function like this:
def update_commission(self):
sale_br = self.env['sale.order']
sale_sr = sale_br.search([]).filtered(lambda sale: sale.date_order.date < self.from_date)
If you are working in odoo's past version like 10,11 than you need to convert this datetime into datetime object because when you call this field it will return date in string format so you need to do fields.Datetime.from_string(sale.date_order.date)

Combine date and time to a datetime

How can I convert a Date and a Time to a DateTime?
Say I have the following date and time
iex> date = ~D[2018-01-01]
iex> time = ~T[00:00:01.000]
How can I combine these to output the datetime: #DateTime<2018-01-01 00:00:01Z> in a clean way?
The best I come up with is using the Timex library:
Timex.add(Timex.to_datetime(date), Timex.Duration.from_time(time))
but I feel that surely there is a nicer, more readable way to combine this.
If you are using the calendar library, you can use either the Calendar.DateTime.from_date_and_time_and_zone function or the Calendar.NaiveDateTime.from_date_and_time function:
iex(4)> Calendar.DateTime.from_date_and_time_and_zone(~D[2018-10-01], ~T[12:22:22], "Australia/Melbourne")
{:ok, #DateTime<2018-10-01 12:22:22+10:00 AEST Australia/Melbourne>}
iex(5)> Calendar.NaiveDateTime.from_date_and_time(~D[2018-10-01], ~T[12:22:22])
{:ok, ~N[2018-10-01 12:22:22]}
There are also from_date_and_time! and from_date_and_time! variants.
You can use NaiveDateTime.new/2:
iex> NaiveDateTime.new(date, time)
{:ok, ~N[2018-01-01 00:00:01.000]}
The reason it is a "naive datetime" instead of a datetime is that the Time struct doesn't contain time zone information. If you know the time zone, you can add that information using DateTime.from_naive/2:
iex> DateTime.from_naive(~N[2018-01-01 00:00:01.000], "Etc/UTC")
{:ok, #DateTime<2018-01-01 00:00:01.000Z>}
While the answer by #legoscia is perfectly valid, here is how you deal with date and time pair (without Timex, just pure Elixir standard library):
date = ~D[2018-01-01]
time = ~T[00:00:01.000]
{Date.to_erl(date), Time.to_erl(time)}
|> NaiveDateTime.from_erl!()
|> DateTime.from_naive("Etc/UTC")
#⇒ {:ok, #DateTime<2018-01-01 00:00:01Z>}

Talend date and time combine

I combine two columns; date and time. When I pass the date and time hot coded it works fine but when I pass it through a column it throws the error:
Unparseable date: "05/05/1992"
I already tried this:
MaterialCodeCSV.xdate == null ?
TalendDate.parseDate("yyyy-MM-dd HH:mm:ss", TalendDate.getDate("yyyy-MM-dd HH:mm:ss")) :
TalendDate.parseDateLocale("yyyy/mm/dd HH:mm:ss",MaterialCodeCSV.xdate.toString() + MaterialCodeCSV.xtime.toString(),"EN");
Java code in Talend:
Date handling can be a bit tricky if using wrong data types. I assume you want to fill a field which is a Date. There are several errors with this way:
MaterialCodeCSV.xdate == null ?
TalendDate.parseDate("yyyy-MM-dd HH:mm:ss", TalendDate.getDate("yyyy-MM-dd HH:mm:ss")) :
TalendDate.parseDateLocale("yyyy/mm/dd H:mm:ss",MaterialCodeCSV.xdate.toString()+ MaterialCodeCSV.xtime.toString(),"EN");
If MaterialCodeCSV.xdate == null you create a date and parse it again instantly? That seems unneccessary complex and inefficient. Change this to TalendDate.getCurrentDate()
Then if xdate is not null, you just concat xdate and xtime, use toString() and try to parse this. Again, this seems unneccesary complex. If I just assume now and xdate and xtime are already Date fields, you could write it as this: MaterialCodeCSV.xdate + MaterialCodeCSV.xtime.
If both are String fields, you have to make sure that xdate is formatted yyyy/MM/dd and xtime is HH:mm:ss. Then you could exclude .toString()
Also, if both are String fields, you have to add an additional space: MaterialCodeCSV.xdate + ' ' + MaterialCodeCSV.xtime
Additionally, in the first case you parse with yyyy-MM-dd HH:mm:ss. In the second case you parse with yyyy/mm/dd H:mm:ss. This reads "year/minute/day". Also there is only one hour digit, not allowing anything after 9:59:59 o'clock to be parsed. Correctly you should use yyyy/MM/dd HH:mm:ss.
So to conclude it should look like this (if I assume correctly and you are using correctly formatted String fields for xdate and xtime):
MaterialCodeCSV.xdate == null ?
TalendDate.getCurrentDate() :
TalendDate.parseDateLocale("yyyy/MM/dd HH:mm:ss", MaterialCodeCSV.xdate + ' ' + MaterialCodeCSV.xtime,"EN");

Day from date Scala

I'd like to get the day from a date object as an integer. This is my code so far.
val dateString = "2015-11-24 23:23:09"
val format = new java.text.SimpleDateFormat("yyyy-MM-dd H:m:s")
val date = format.parse(dateString)
print(date) # this gives Tue Nov 24 23:23:09 CST 2015
Now, from date, I want to get the day of the month as an integer. How do I do that?
Thanks.
Just use the Java Calendar class (although I would recommend moving over to the Joda library if you are doing much serious work with dates/times):
val cal = Calendar.getInstance()
cal.setTime(date)
val dayOfMonth = cal.get(Calendar.DAY_OF_MONTH)
tl;dr
LocalDateTime.parse(
"2015-11-24 23:23:09".replace( " " , "T" )
).getDayOfMonth()
java.time
The modern approach uses the java.time classes.
Using Java syntax here as I don't know Scala. Note that java.time uses immutable objects.
Convert your string to comply with ISO 8601 standard format with a T in the middle.
String input = "2015-11-24 23:23:09".replace( " " , "T" ) ;
Parse as an LocalDateTime as your input lacks any time zone or offset-from-UTC.
LocalDateTime ldt = LocalDateTime.parse( input ) ;
Interrogate for the day of month.
int dom = ldt.getDayOfMonth() ;
Using Joda,
org.joda.time.DateTime.now().getDayOfMonth()
or equivalently,
import java.util.Date
new org.joda.time.DateTime(new Date()).getDayOfMonth