Mule - Date format from m/dd/yy to yyyy-mm-dd - date

want to convert date to the above required format. Is it possible to write a groovy script to do so in datamapper? If yes can you please give an example. Or can i reference another expression or component?

I think the simplest way to do what you need is to edit the datamapper script and replace the line where you perform the date mapping for this one:
output.date = date2str(str2date(input.date,"d/mm/yy"), "yyyy-mm-dd");
but replacing the input and output fields accordingly.

Here is the link which explains how to do it in java. You may easily groovify it.
Here the script
import java.text.SimpleDateFormat
def OLD_FORMAT = "m/dd/yy"
def NEW_FORMAT = "yyyy-MM-dd"
// August 12, 2010
def oldDateString = "8/12/10"
def sdf = new SimpleDateFormat(OLD_FORMAT)
def d = sdf.parse(oldDateString)
sdf.applyPattern(NEW_FORMAT)
newDateString = sdf.format(d)
println "Date in modified format : $newDateString"
EDIT:
Changed as per source format based on comment

#[server.dateTime.format('yyyy-MM-dd')]

I'm not a fan of such nesting.
I prefer a variation on the above answers, where I only keep the inner function:
output.date = str2date(input.date,"m/dd/yy")
but then defined the output format in the datamapper's graphical editor, by setting the field to a date with format "yyyy-MM-dd".
Both of these approaches work, but I find this method easier to read and far easier to explain to others.

You can also use format method in the expression to set the date as below :
flowVars.Timestamp = server.dateTime.format("YYYYMMDDHHmm");

Related

Matlab - isbusday and busdate format change

Would you be able to advise how can i change the format of the date the functions isbusday and busdate are using ?
The functions use US date format by default, but I need them to be in European format dd/mm/yyyy.
I have attempted to use the code below but it is not working.
isbusday('01-01-2015','dd-mm-yyyy')
busdate('01-01-2015','dd-mm-yyyy',1)
Thank you very much.
You want to convert your string to a datetime object. That is where you can control the format:
d = datetime('01-01-2015','InputFormat','dd-mm-yyyy');
isbusday(d)
busdate(t)
See the documentation: https://www.mathworks.com/help/matlab/ref/datetime.html and https://www.mathworks.com/help/finance/isbusday.html

Format date and add month to it

I'm currently working with embarcadero c++, this is the first time I'm working with it so it's completely new to me.
What I'm trying to achieve is to get the current date, make sure the date has the "dd/MM/yyyy" format. When I'm sure this is the case I want to add a month to the current date.
So let's say the current date is 08/18/2016 this has to be changed to 18/08/2016 and then the end result should be 18/09/2016.
I've found that there is a method for this in embarcardero however I'm not sure how to use this.
currently I've only been able to get the current date like this.
TDateTime currentDate = Date();
I hope someone will be able to help me out here.
I figured it out.
After I've searched some more I found the way to use the IncMonth method on this page.
The example given my problem is as follows:
void __fastcall TForm1::edtMonthsExit(TObject *Sender)
{
TDateTime StartDate = edtStartDate->Text;
int Months = edtMonths->Text.ToInt();
TDateTime NextPeriod = IncMonth(StartDate, Months);
edtNextPeriod->Text = NextPeriod;
}
After looking at I changed my code accordingly to this
TDateTime CurrentDate = Date();
TDateTime EndDate = IncMonth(CurrentDate, 1);
A date object doesn't have a format like "dd/MM/yyyy". A date object is internally simply represented as a number (or possibly some other form of representation that really isn't your problem or responsibility).
So you don't have to check if it's in this format because no date objects will ever be in this format, they simply don't have a format.
You will have to do additions/subtractions on the Date object that the language or library gives you, THEN (optionally) you can format it to a human-readable string so it looks like 18/08/2016 or 18th of August 2016 or whatever other readable format that you choose.
It might be that the TRANSFER of a date between 2 systems is in a similar format, but then formatting the date like that is entirely up to you.
As for how to do that, the link you posted seems like a possible way (or alternatively http://docwiki.embarcadero.com/Libraries/Berlin/en/System.SysUtils.IncMonth), I'm afraid I can't give you an example as I'm not familiar with the tool/language involved, I'm just speaking generically about Date manipulations and they should ALWAYS be on the raw object.

Trying to get the current date with a specific format but Date is coming with a expected way

Here is the code and output. Please let me know what's wrong with the code.
import java.text.SimpleDateFormat
def myDate=new Date()
def sdf= new SimpleDateFormat("MM/DD/YYYY")
return sdf.format(myDate)
log.info sdf.format(myDate)
Op-: 04/94/2016
Thanks!
You need lower case dd AND yyyy
You can also call format on dates directly in groovy
date.format('MM/dd/yyyy')
Also, if this is to be read by anyone outside the US, or you want to be able to sort dates alphabetically, consider the more universal (iso8601) format of
date.format('yyyy-MM-dd')
As AR.3 said in their answer, the documentation for simpledateformat can be found here

Joda-time parser with instance-local default timezone

Is there an "instance-local" version of Joda's DateTimeZone.setDefault? I'm trying to achieve something like this:
val parser = new ParserWithDefaultTimezone("GMT+1");
parser.parse("1970-01-01 00:00:00").getMillis // -3600000L
parser.parse("1970-01-01 00:00:00 UTC").getMillis // 0L
Without polluting anything global. All I can find in the Joda docs is something that (ironically) modifies global state.
If there is a non-Joda solution, I am interested in that, too.
EDIT: Forgot to mention that if there is no ready-to-use class that does this, I'll settle for: "what is the easiest way to see if a time string contains an explicit timezone?" I can't distinguish between an explicit one and a timezone that Joda set by default.
EDIT 2: I don't have a format string to feed; I'm looking for something that infers the format at runtime.
You can use withZone to alter the date/time zone used:
val fmt = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss ZZZ").withZone(DateTimeZone.forID("Europe/Berlin"))
val date = fmt.parseDateTime(???);
The set-up required to make the time zone optional is a little bit more complicated:
val tz = new DateTimeFormatterBuilder().appendLiteral(" ").appendTimeZoneId().toFormatter()
val fmt = new DateTimeFormatterBuilder()
.append(DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"))
.appendOptional(tz.getParser())
.toFormatter().withZone(DateTimeZone.forID("Europe/Berlin"))
println(fmt.parseDateTime("1970-01-01 12:00:00 UTC"))
println(fmt.parseDateTime("1970-01-01 12:00:00 Europe/Berlin"))
println(fmt.parseDateTime("1970-01-01 12:00:00"))
As long as your remark
I don't have a format string to feed; I'm looking for something that infers the format at runtime.
applies only with respect to the time zone, solution 2 might do what you want. If, on the other hand, you really don't know, in wich format the dates are provided (dd/mm/yyyy vs. mm/dd/yyyy vs. yyyy/mm/dd vs. whatever), then I think you are out of luck: such a conversion would be ambiguous at best. Is 01/03/2015 the 1st of March or the 3rd of January?

Why do I need to parse dates in Grails?

I am in the unfortunate position that I need to use a composite id in a Grails app where I work with legacy data. This means I have to override some actions in the controller, but as I did this I was struck by the fact that I could not use use a date argument directly as a parameter to a dynamic method.
Instead of just doing MyLegacyObj.findBySystemIdAndLogDate(params.systemId, params.logDate), I first needed to parse the date string before giving it to the dynamic method. To further complicate matters I had no idea what format the date string had (until I added lots of log.debug() string to the output). So now I have a bit of code looking like this
def formatter = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy")
MyLegacyObj.findBySystemIdAndLogDate(params.systemId, formatter.parse(params.logDate));
This feels unoptimal, no to say dangerous (what if the date format changes with the locale?)? What would be a recommended way of doing this, and do I really need to parse dates at all?
Date is a pretty complex object and params are just Strings, so Date is submitted in parts. It is "magically" assembled from the parts when assigning x.properties = params.
Command object will do the work for you, if you add a Date field to it.
It has nothing to do with methods' dynamic or static invocation. Your GSP that renders Date editor might interfere too.