Vaadin - PopupDateField not accepting multiple input date formats - date

In my project I have a standard DateField format as "dd MMM yyyy". I used setDateFormat("dd MMM yyyy") to se this format. But now users want to enter "MM/dd/yyyy", "MM-dd-yyyy" and "MM dd yyyy" formats too, with the displayable date MUST still be "dd MMM yyyy".
Right now when I enter "31/01/2016" in the DateField with setDateFormat("dd MMM yyyy") I am getting "date format not recognized" error.
My question is how can I make a datefield accept multiple date format inputs(not using the calendar picker).
Any help is much appreciated. Thanks for reading the post!!!

You can override a method handleUnparsableDateString:
public class MyDateField extends DateField {
#Override
protected Date handleUnparsableDateString(String dateString) throws Converter.ConversionException {
return super.handleUnparsableDateString(dateString);
}
}
That method is called when DateField is not able to parse the input. You can parse the input in the method and return a correct Date instance.

Related

Google scripts - Formatting date from a form input

To preface I'm very new at this but I currently have a script that outputs a Google Doc/PDF based on inputs into my google form.
The PDF is output whenever a new form is submitted, currently the formatting for the date comes out as DD/MM/YY but I want it to appear as dd MMMM yyyy i.e. 19 May 2020 rather than 19/05/20.
I've tried using the following to change the formatting
var signDate = e.values[1];
var formatSignDate = Utilities.formatDate(signDate, "GMT+1", "dd MMMM yyyy");
body.replaceText('{{Execution Date}}', formatSignDate);
But I get the following error
Exception: The parameters (String,String,String) don't match the method signature for Utilities.formatDate.
at autoFillFromForm(Code:17:28)
What am I doing wrong? Can I not use .formatDate with the date from my sheets cells?
Edit: error message with log of signDate
Stackdriver logs Info signDate = 20/05/2020 Error Exception: The parameters
(String,String,String) don't match the method signature for
Utilities.formatDate. at autoFillFromForm(Code:23:34)
Exception: The parameters (String,String,String) don't match the method signature for Utilities.formatDate
means that signDate is not recognized correctly as a date object
Please provide a log of signDate to help you troubleshoot more in detail.
UPDATE
If you have a date string in UK format ( DD/MM/YYYY), it won't be automatically recognized as a Javascript date object.
You'll need to convert it, e.g:
var convertedDate = new Date(signDate.split('/')[2], signDate.split('/')[1] - 1, signDate.split('/')[0]);
Logger.log(convertedDate);
var formatSignDate = Utilities.formatDate(convertedDate, "GMT+1", "dd MMMM yyyy");
Logger.log(formatSignDate);
Note: Using Utilities.formatDate() as above might give you the wrong date if you are setting it to a different timezone than your script time zone.

Ionic Datetime picker timezone issue

I have this date getting from an API Call in a JSON Format: CreatedOn : 2018-08-27T05:11:29.000Z
When I open the form, I am setting the datetime picker to that field.
<ion-datetime displayFormat="MMM DD YYYY h:mm A" pickerFormat="MMM DD YYYY h:mm A"
[(ngModel)]="Task.CreatedOn"></ion-datetime>
Getting here Aug 27 2018 5:11 AM which is wrong
I am also showing the same date with angular.
<h6>{{Task.CreatedOn | date : 'MMMM d,yyyy At hh:mma' }}</h6>
Getting here August 27,2018 At 08:11AM Which is right.
I think there is a timezone issue with the datetime picker when I am setting it.
How can I solve this issue?
You can use Angular formatToLocal pipe which will internally convert using moment.
<ion-datetime
displayFormat="MMM DD YYYY h:mm A"
pickerFormat="MMM DD YYYY h:mm A"
[ngModel]="Task.CreatedOn | formatToLocal"
(ngModelChange)="Task.CreatedOn=$event">
</ion-datetime>
PIPE:
import {Pipe, PipeTransform} from '#angular/core'
import moment from 'moment';
#Pipe({ name: 'formatToLocal'})
export class FormatToLocal implements PipeTransform{
transform(val) {
if(val){
return moment(val).format();
}
}
}

Passing date in Date data type only in groovy

I have been struggling with this even after doing so much of research on such a simple thing, so I need some help here.
I need to pass current date in date data type only in 'yyyy-MM-dd' format. SimpleDateFormat converts current date to string type and while trying to parse though it gets converted to Date type but changes the format.
I need currentDate in the format 'yyyy-MM-dd' of Date Type.
if(!session.dtfromDate && !session.dttoDate)
eq("startDate", currentDate)
I have managed to figure out a solutions to this. First got my desired format which obviously converted it to a String and then parsed this to a Date. It has worked perfectly fine.
SimpleDateFormat nsdf = new SimpleDateFormat('yyyy-MM-dd')
String currentDate = new SimpleDateFormat('yyyy-MM-dd').format(new Date());
Date newDate = (Date)nsdf.parse(currentDate)
if(!session.dtfromDate && !session.dttoDate)
eq("startDate", newDate)

Joda Time: how to parse time and set default date as today's date

I have to parse dates in formats:
HH:mm
dd MMM
dd MMM yyyy
I've managed to handle the last two of them:
val dateParsers = Array(
DateTimeFormat.forPattern("dd MMM").getParser,
DateTimeFormat.forPattern("dd MMM yyyy").getParser,
ISODateTimeFormat.timeParser().getParser
)
val formatter = new DateTimeFormatterBuilder().append(null, dateParsers).toFormatter.withZoneUTC
DateTime.parse(updatedString, formatter.withDefaultYear(currentYear).withLocale(ruLocale))
Everything is ok with dd MMM and dd MMM yyyy, but when I'm trying parse time like 05:40 I'm getting 01-01-1970 date instead of today's date. What is the simplest method to set default date as today's date in parser?
Joda-Time-Formatter only supports withDefaultYear(), not things like withDefaultDate(). Instead you can do this:
DateTimeFormatter timeParser = DateTimeFormat.forPattern("HH:mm");
LocalTime time = timeParser.parseLocalTime("05:40");
DateTimeZone tz = DateTimeZone.getDefault(); // Or DateTimeZone.UTC or DateTimeZone.forID( "Europe/Paris" )
LocalDate today = LocalDate.now(tz);
DateTime moment = today.toLocalDateTime(time).toDateTime(tz);
System.out.println(moment);
// output in my local timezone: 2014-08-20T05:40:00.000+02:00
Note: I have written the solution in Java, because I am not a scala-guy.
Date and Time are completely different, without subclassing DateTimeFormatter and implementing your special "time at todays' date"-algorithm you wont get very far. Either subclass or maybe inject your current date into the string if it matches some regular expression
I'd use withDate()
private static final DateTimeFormatter FORMATTER = DateTimeFormat.forPattern("HH:mm");
#Test
public void test() {
DateTime dateTime = FORMATTER.parseDateTime("05:40");
DateTime now = new DateTime();
dateTime = dateTime.withDate(now.getYear(), now.getMonthOfYear(), now.getDayOfMonth());
System.out.println(dateTime.toDate());
}

DateTime format not formatting correctly

I have a datetime format in XML and I'm trying to unmarshall the values as follows:
2013-03-17T19:12:14Z -> 2013-03-17 19:12 +0100
I have used Joda's DateTime and a DateTimeAdapter class to override the unmarshalling. The datetime format is coming out weird, as follows:
{"iMillis":1363510800000,"iChronology":{"iBase":{"iBase":{"iBase":
{"iMinDaysInFirstWeek":4}},"iParam":{"iZone":{"iTransitions":
[-9223372036854775808,-3852662325000,-1691964000000,-1680472800000,
-1664143200000,-1650146 400000,-1633903200000,-1617487200000,
-1601848800000,- etc etc.
Can anyone help me format this date?
I was unable to figure out the answer so I've tried the following:
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm Z");
DateTime dateTime = new DateTime(v);
long dateTimeMiliSec = dateTime.getMillis();
Date date = new Date(dateTimeMiliSec);
return sd.format(date);
So 2013-03-17T09:00:00Z converts to 2013-03-17 09:00 +0000