JSP Database setTime, get a time from form - database

// Create the prepared statement and use it to
// INSERT the student attributes INTO the Student table.
PreparedStatement pstmt = conn.prepareStatement(
"INSERT INTO Review VALUES (?, ?, ?, ?)");
pstmt.setInt(1, Integer.parseInt(request.getParameter("section_id")));
pstmt.setString(2, request.getParameter("quarter"));
pstmt.setInt(3, Integer.parseInt(request.getParameter("year")));
pstmt.setTime(4, ????)
Can anyone tell me how can i read values with the type Time?? Lets say time like "16:34"? How can I define a pattern and read it?

You need to do this.
public static java.sql.Time getCurrentJavaSqlTime(String time) {
Date date = new SimpleDateFormat("HH:mm", Locale.ENGLISH).parse(time); //Convert to Date
return new java.sql.Time(date.getTime()); //Now to time
}
java.sql.Time time = getCurrentJavaSqlTime("16:34");
System.out.println("time=" + time);
pstmt.setTime(4,time);
Use format patterns to work with yout String.
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
m Minute in hour Number 30
To obtain from user try this
java.sql.Time time = getCurrentJavaSqlTime((String)request.getParameter("time"));
pstmt.setTime(4,time);

Related

How to check if a string date has passed a certain date

I have these strings:
yy = "19";
mm = "05";
dd = "31";
These represent the creation date of a certain object in my project. This object expires after one month. How do I check if the object has expired already?
(I came across this solution but thought there might be another way to do it.)
UPDATE: the string date apparently represent the actual expiry date
I ended up using the date format "yymmdd" so I can convert it to type long and do a simple number comparison.
sprintf(buffer, "%s%s%s", yy, mm, dd);
expiryDate = atol(buffer);
// get current date of format "yymmdd" as well
// getCurrentDate() is my function that gets the date from my SDK
currentDate = getCurrentDate();
if(expiryDate >= currentDate)
{
// expired object!
}

How to add days to date time in Salesforce Apex?

Hi I am using Salesforce Apex,
I have a date as String as below. I need to add days to it using Apex.
String dateTime = '2017-07-08T23:59:59Z';
If I add one day to it then it should be 2017-07-09T23:59:59Z as string. How will I do this?
Thanks!
Beware the DST issue! The "addDays" function is not DST-aware, so if you step over a DST transition during the addition of days (in a time zone that has DST) then the time will be messed up.
To resolve this one split the date/time into separate date and time parts first, add the days to the date part then re-combine at the end, like:
DateTime dt = ...;
Integer days = ...;
Date d = dt.date().addDays(days);
Time t = dt.time();
dt = DateTime.newInstance(d, t);
If you are working in the UK (London) time zone the following anonymous Apex illustrates the issue nicely:
DateTime dt = DateTime.newInstance(2017, 10, 28, 23, 59, 59);
System.debug('Adding days directly: ' + dt.addDays(2));
Date d = dt.date().addDays(2);
Time t = dt.time();
dt = DateTime.newInstance(d, t);
System.debug('Adding days in parts: ' + dt);
You need to convert the string to a DateTime and then add days. You can format it back after
String stringDateTime = '2017-07-08T23:59:59Z';
DateTime dt = DateTime.valueOfGmt(stringDateTime);
DateTime tomorrow = dt.addDays(1);
DateTime nextMonth = dt.addMonths(1);
DateTime anniversary = dt.addYears(1);
String formattedDateTime = dt.format('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');

salesforce SOQL can't get datetime in local time

My objective is to count Lead records based on simple date range query in local time
Integer TotalLeads = [ Select Count() From Lead where createddate >= fromdate CreatedDate <= todate];
Fairly basic. However, the issue I'm running into is I only want to count the leads for the "local" time not UTC; createddate is in UTC for lead records.
Sample dates:
From: 03/23/2017
To: 03/29/2017
For these sample dates and my local time is UTC - 7 (Los Angeles), so my query would be
Integer TotalLeads = [ Select Count() From Lead where createddate >= 2017-03-23T07:00:00z AND CreatedDate <= 2017-03-30T06:59:59z];
If these are my dates, how do I append the local time so from date is 2017-03-23T07:00:00z and to date is 2017-03-30T06:59:59z?
Using from date first, I was able to do the following but can't figure how to keep it in local time
// Date
date ds = date.valueof('2017-03-23');
string dm = string.valueOf( ds.month());
string dd = string.valueOf(ds.day());
string dy = string.valueOf(ds.year());
// DateTime Midnight (UTC)
String SDate = string.valueof(dm +'/' + dd + '/' + dy + ' 12:00 AM');
system.debug(SDate); // -> 3/23/2017 12:00 AM
// DateTime (Local Time)
datetime ds2 = datetime.parse(SDate );
system.debug(ds2); // -> 2017-03-23 07:00:00
system.debug(ds2.format('yyyy-MM-dd\'T\'hh:mm:ss'')); // -> 2017-03-23T12:00:00
As you can see, using ds2.format put its in the format I need but back to UTC (midnight), I need it to be 2017-03-23T07:00:00
Any suggestions?
Thanks!
Figured what I was doing wrong. The date calculation was fine, it had to do with how this was being passed to a batch job.

Converting day, weeknr and year to date

I've been given an excel document in which worktime information is noted, this document contains certain columns which are being read by using SSIS in visual studio, after that the information is writen to a Database.
The week and year column contain the week number and the year, the columns Monday up to Friday contain information about how many working hours have been spent on a certain task on that day of the week.
What I'd like to do is take the WeekNr, Year and Day and convert these into a date. I've been trying to accomplish this by using a script component that converts a day number, week number and year to a date but so far I haven't been able to get the day number from the columns. In my opinion it would work best if used with a start and end date taking the first and last date of that week.
So my question is if someone knows how to accomplish this, or if I should try a different approach.
The script component:
public override void Input0_ProcessInputRow(Input0Buffer Row, CultureInfo cultureInfo, int day )
{
DateTime firstDayOfYear = new DateTime(Int32.Parse(Row.Jaar), 1, 1);
int firstWeek = cultureInfo.Calendar.GetWeekOfYear(firstDayOfYear, cultureInfo.DateTimeFormat.CalendarWeekRule, cultureInfo.DateTimeFormat.FirstDayOfWeek);
int dayOffSet = day - (int)cultureInfo.DateTimeFormat.FirstDayOfWeek + 1;
Row.TaskDates = firstDayOfYear.AddDays((Int32.Parse(Row.Week) - (firstWeek + 1)) * 7 + dayOffSet + 1);
}
Based on this answer, I think you want something like the following. This result gives you Monday's date, so you can just AddDays based on the column day of the week.
DateTime jan1 = new DateTime(Int32.Parse(Row.Jaar), 1, 1);
int daysOffset = DayOfWeek.Monday - jan1.DayOfWeek;
DateTime firstMonday = jan1.AddDays(daysOffset);
var cal = CultureInfo.CurrentCulture.Calendar;
int firstWeek = cal.GetWeekOfYear(firstMonday, CalendarWeekRule.FirstFullWeek, DayOfWeek.Monday);
var weekNum = Int32.Parse(Row.Week);
if (firstWeek <= 1)
{
weekNum -= 1;
}
var mondaysDate = firstMonday.AddDays(weekNum * 7);
var tuesdaysDate = mondaysDate.AddDays(1);

how can i create in vb.net a reminder of a task every two weeks

I want to create a task reminder (which the user add it to the database with a start date spot) every two weeks from the date already entered in the database. Based on the position of the day in the week, I made a try but to no avail, if someone has an idea I would be grateful
cmd5.CommandText = "select * from task where frequence='2W' "
da5.SelectCommand = cmd1
da5.Fill(ds5, "0")
DataGridView6.Columns.Add("fr", "fr")
DataGridView6.Columns.Add("class", "class")
For i As Integer = 0 To ds5.Tables(0).Rows.Count - 1
If Date.Now.DayOfWeek > 3 Then
If CType(ds5.Tables(0).Rows(i).Item(3), Date).DayOfWeek = Date.Now.DayOfWeek Or CType(ds5.Tables(0).Rows(i).Item(3), Date).DayOfWeek - ???? = Date.Now.DayOfWeek Then
DataGridView6.Rows.Add(ds5.Tables(0).Rows(i).Item(4), CType(ds5.Tables(0).Rows(i).Item(3), Date).DayOfWeek)
End If
End If
If Date.Now.DayOfWeek < 4 And Date.Now.DayOfWeek <> 0 Then
If CType(ds5.Tables(0).Rows(i).Item(3), Date).DayOfWeek = Date.Now.DayOfWeek Or CType(ds5.Tables(0).Rows(i).Item(3), Date).DayOfWeek + ???? = Date.Now.DayOfWeek Then
DataGridView6.Rows.Add(ds5.Tables(0).Rows(i).Item(4), CType(ds5.Tables(0).Rows(i).Item(3), Date).DayOfWeek)
End If
End If
Next
in "??????" I don't know what i can put to obtain the correct results !! can someone help me please !
As I understood from your comments, you are interested just in the interval-analysis part, that is, in determining whether today's date is before/after the given conditions as defined in your database (with start_date and frequency). Here you have a sample code to take care of this:
Dim start_date As Date = Now.AddDays(-14) 'start_date value to be taken from the DB
Dim frequency As TimeSpan = TimeSpan.FromDays(14) 'Number of days as a function of the given frequency in your DB; this is for 2W.
Dim elapsedDays As TimeSpan = Now.Subtract(start_date)
If (elapsedDays >= frequency) Then
'NOTIFY USER -> it has passed, at least, "frequency" number of days from "start_date"
If (elapsedDays > frequency) Then
Dim addDays As Integer = elapsedDays.Subtract(frequency).Days
Dim dateWhenTargetMet As Date = Now.AddDays(-addDays)
'Tell user that the target was met "addDays" ago, on "dateWhenTargetMet"
End If
End If
The code above checks whether the current date equals (or is later than) the start_date + frequency number of days.

Resources