why no result when change month in sqflite and flutter? - sql

when i run this code in same month it work correctly but when the dates not a same month i don't have result []
for example
start date: 2023-01-28T00:00:00.000
end date: 2023-01-30T00:00:00.000
work correctly and
start date: 2023-01-28T00:00:00.000
end date: 2023-02-02T00:00:00.000
not working
Future<List> tPeriod(String startDate, String endDate) async{
var start = DateFormat('yMMMd HH:mm').parse(startDate);
print('start date: $start');
var end = DateFormat('yMMMd HH:mm').parse(endDate);
print('end date: $end');
int comparison = start.compareTo(end);
print('Comparison result: $comparison');
if (comparison > 0 ) {
print('Error: Start date is after end date');
return [];
}
var db = await database;
var result = await db.rawQuery("SELECT $colName FROM $Table WHERE $colDate BETWEEN '$startDate' AND '$endDate' ORDER BY RANDOM() LIMIT 1");
print('$result');
return result.toList();
}

Related

How to calculate date in kotlin with the given format?

How to see if a user can be unbanned or not between 2 dates?
Hi, I have a given variable which is the date the user was unbanned as end_banned in the format HH:mm:ss dd/MM/YYYY.
val end_banned: String= "15:05:00 12/01/2022"
I want to calculate if at the current time they can be unbanned or not. I have tried with SimpleDateFormat, Calendar, Date... but still haven't found a solution.
I've tried separating each element of seconds, minutes, days... and comparing them with if...else like this:
var cal = Calendar.getInstance()
cal.timeZone = TimeZone.getTimeZone("Asia/Ho_Chi_Minh")
var _hours = cal.get(Calendar.HOUR)
var _minutes = cal.get(Calendar.MINUTE)
var _seconds = cal.get(Calendar.SECOND)
var _day = cal.get(Calendar.DAY_OF_MONTH)
var _month = cal.get(Calendar.MONTH) + 1
var _year = cal.get(Calendar.YEAR)
var hours = end_banned.toString().substring(0, 2).toInt()
var minutes = end_banned.toString().substring(3, 5).toInt()
var seconds = end_banned.toString().substring(6, 8).toInt()
var day = end_banned.toString().substring(9, 11).toInt()
var month = end_banned.toString().substring(12, 14).toInt()
var year = end_banned.toString().substring(15).toInt()
if (_year >= year && _month >= month && _day >= day && _hours >= hours && _minutes >= minutes && _seconds >= seconds
|| _year >= year && _month >= month && _day >= day && _hours >= hours && _minutes >= minutes
|| _year >= year && _month >= month && _day >= day && _hours >= hours
|| _year >= year && _month >= month && _day >= day
|| _year >= year && _month >= month
|| _year >= year) {
println("True")
} else {
println("False")
}
But it is only true for the first 3 conditions when there are hours, minutes and seconds.
I tried with SimpleDateFormat and Date like this:
var cal = Calendar.getInstance()
cal.timeZone = TimeZone.getTimeZone("Asia/Ho_Chi_Minh")
var hours = cal.get(Calendar.HOUR)
var minutes = cal.get(Calendar.MINUTE)
var seconds = cal.get(Calendar.SECOND)
var day = cal.get(Calendar.DAY_OF_MONTH)
var month = cal.get(Calendar.MONTH) + 1
var year = cal.get(Calendar.YEAR)
var sdf = SimpleDateFormat("HH:mm:ss dd/MM/yyyy")
var sdf_unbanned = sdf.parse(end_banned)
var sdf_now = sdf.parse("${hours}:${minutes}:${seconds} ${day}/${month}/${year}")
if (sdf_now.time - sdf_unbanned.time <= 0) {
println(true)
} else {
println(false)
}
But this condition always gives an incorrect number if I adjust the now and unbanned variables a few minutes apart (This makes it easier to spot)
I did not fully understand your question Sir, But I assume you want to compare unbanned_date to the current date (now time) if they are the same(we reached the unban date) then it should unban whatever you are unbanning, If so the implementation requires less code to achieve that ,Like this :
val sdf = SimpleDateFormat("dd/MM/yyyy")
val strDate: Date = sdf.parse(end_banned)
if (System.currentTimeMillis() > strDate.getTime()) {
urbanUser = true // or what ever your logic is
}
Shame, I got confused between cal.get(Calendar.HOUR) and cal.get(Calendar.HOUR_OF_DAY)
Because cal.get(Calendar.HOUR) returns only 12h format
And cal.get(Calendar.HOUR_OF_DAY) will return 24h format
I found a workaround and here is the code that I fixed and tested:
var cal = Calendar.getInstance()
cal.timeZone = TimeZone.getTimeZone("Asia/Ho_Chi_Minh")
var sdf = SimpleDateFormat("HH:mm:ss dd/MM/yyyy")
var hour = cal.get(Calendar.HOUR_OF_DAY)
var minute = cal.get(Calendar.MINUTE)
var second = cal.get(Calendar.SECOND)
var day = cal.get(Calendar.DAY_OF_MONTH)
var month = cal.get(Calendar.MONTH) + 1
var year = cal.get(Calendar.YEAR)
var now: Date? = sdf.parse("${hour}:${minute}:${second} ${day}/${month}/${year}")
var end: Date? = sdf.parse(end_banned)
return now?.time!! > end?.time!!
Thanks for the help.

How do I extract current date, month and year in the format yyyy-mm-dd in karate dsl?

* def date =
"""
function(s) {
var SimpleDateFormat = Java.type('java.text.SimpleDateFormat');
var sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
return sdf.parse(s).time;
}
"""
Currently, I'm using this. Any help is much appreciated.
You can also archive the following by using javascript as follows:
* def todaysDate =
"""
function()
{
var d=new Date();
var month = String(d.getMonth() + 1);
var day = String(d.getDate());
var year = String(d.getFullYear());
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return year+'/'+month+'/'+day
}
"""
* print todaysDate()
Here you go:
* def today =
"""
function() {
var SimpleDateFormat = Java.type('java.text.SimpleDateFormat');
var sdf = new SimpleDateFormat('yyyy-MM-dd');
return sdf.format(new java.util.Date());
}
"""
* print today()

Date formatting in Karate

I have the the below date returned from the SQL query in Karate feature file:
2020-01-31 00:00:00.0
I need to convert it to: 31-JAN-20 format. I have tried the below:
* def effectiveDt =
"""
function(s) {
var SimpleDateFormat = Java.type('java.text.SimpleDateFormat');
var sdf = new SimpleDateFormat("dd-mon-yy");
return return sdf.format(s)
}
"""
but its not working for me.
but the below worked and returns 31-00-19, but I want 31-JAN-20 format
var sdf = new SimpleDateFormat("dd-mm-yy");
Any help would be appreciated!
Here is an example that worked for me:
* def getSubtractedYear =
"""
function(s) {
var DateTimeFormatter = Java.type("java.time.format.DateTimeFormatter");
var LocalDate = Java.type("java.time.LocalDate");
var ChronoUnit = Java.type("java.time.temporal.ChronoUnit");
var dtf = DateTimeFormatter.ofPattern("MM/dd/yyyy");
try {
var adj = LocalDate.parse('02/02/2020', dtf).minusMonths(12);
return dtf.format(adj);
} catch(e) {
karate.log('*** date parse error: ', s);
}
}
"""
And call like:
* string subtracted = call getSubtractedYear aDate

Got wrong date when converting string date retrieved from the api

I'm calling an API to get some dates, but the API returns the date in string format so I'm using new Date() to convert it to date format, the problem is that after the conversion I'm getting a wrong date
For example new Date("2019-04-19") is returning Thu Apr 18 19:00:00 GMT-05:00 2019
this is my code
function HebrewCalAPI(fechas)
{
var response = UrlFetchApp.fetch("https://www.hebcal.com/hebcal/?v=1&cfg=json&maj=on&min=on&mod=on&nx=on&year=now&month=x&ss=on&mf=on&c=on&geo=geoname&geonameid=3530597&m=50&s=on");
var data = JSON.parse(response.getContentText());
var arrayEmpiezan = []
for(var i in fechas)
{
for(var i2 in data.items)
{
if(data.items[i2].title == fechas[i])
{
arrayEmpiezan.push(new Date(data.items[i2].date));
}
}
}
return arrayEmpiezan
}
var fiestasEmpiezan = ["Erev Pesach","Pesach VI (CH''M)","Erev Shavuot","Erev Rosh Hashana","Erev Yom Kippur","Erev Sukkot","Sukkot VII (Hoshana Raba)"]
var fiestasAcaban = ["Pesach II", "Pesach VIII","Shavuot II","Rosh Hashana II","Yom Kippur","Sukkot II","Simchat Torah"]
var FiestaEmpieza = date
var FiestaAcaba = date
FiestaEmpieza = HebrewCalAPI(fiestasEmpiezan)
FiestaAcaba = HebrewCalAPI(fiestasAcaban)
Any help please ?

Refreshing view panel from dojo widget

Got this code from #MichaelSaiz and altered it slightly, and the calendar widget looks/works great with the Calendar.css that comes with dojo 1.5.x on the Domino server.
However, I need to refresh a Calendar view when the user clicks on a date, and although fields are being refreshed OK, the view is behaving strangely, and I can't see why?
XSP.addOnLoad(function(){
dojo.require("dojox.widget.Calendar");
dojo.require("dojo.date","dijit.registry");
dojo.require("dojo.date.locale");
dojo.require("dijit.Calendar");
dojo.ready(function(){
// create the Calendar:
var selectedDate = null;
var calendar_body = new dojox.widget.Calendar({
value: new Date(),
onValueSelected: function(date){calendarDateClicked(date);
}
}, "calendar_body");
//create Click action
function calendarDateClicked(date){
var d = new Date(date);
var month = '' + (d.getMonth() + 1);
var day = '' + d.getDate();
var year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
var dateString = [day,month,year].join("/");
var y = dojo.date.locale.format(d, {datePattern:"dd/MM/yyyy", selector: 'date'});
//dojo.byId('#{id:hiddenCalWidgetSelectedDate}').value = dateString
dojo.byId('#{id:hiddenCalWidgetSelectedDate}').value = y;
dojo.byId('#{id:calDate}').value = y;
//dojo.byId('#{id:calDate}').value = dateString;
XSP.partialRefreshGet("#{id:dayPanel1}",{});//Post Value to server
}
});
});
This fires when the user clicks the calendar and it updates a field (calDate) and then updates the viewPanel (dayPanel1).
The view is filtered based on a calDate field which clicking on the calendar sets.
Any ideas how I can get the view refreshed when the user clicks on the calendar? Seems basic but it's driving me nuts!!
Graeme
The date format was incorrect (as #MichaelSaiz surmised). Got it sorted now.
XSP.addOnLoad(function(){
dojo.require("dojox.widget.Calendar");
dojo.require("dojo.date","dijit.registry");
dojo.require("dojo.date.locale");
dojo.require("dijit.Calendar");
dojo.ready(function(){
// create the Calendar:
var selectedDate = null;
var calendar_body = new dojox.widget.Calendar({
value: new Date(),
onValueSelected: function(date){calendarDateClicked(date);
}
}, "calendar_body");
//Set month in correct format
function setMonth(month){
switch(month)
{
case 1:
month = "Jan";
break;
case 2:
month = "Feb";
break;
}
return month;
}
//create Click action
function calendarDateClicked(date){
var d = new Date(date);
var month = (d.getMonth() + 1);
month = setMonth(month);
var day = '' + d.getDate() + ",";
var year = d.getFullYear();
//if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
var dateString = [month,day,year].join(" ");
//var y = dojo.date.locale.format(d, {datePattern:"dd/MM/yyyy", selector: 'date'});
dojo.byId('#{id:hiddenCalWidgetSelectedDate}').value = dateString
//dojo.byId('#{id:hiddenCalWidgetSelectedDate}').value = y;
//dojo.byId('#{id:calDate}').value = y;
dojo.byId('#{id:calDate}').value = dateString;
XSP.partialRefreshPost("#{id:mainPanel}",{});//Post Value to server
}
});
});