J2ME: Get only year/month/day from DateField - java-me

I'm trying to use dateField to display which year, month or day has been selected in the calendar.
public DateField getDateField() {
dateField = new DateField("dateField", DateField.DATE);
dateField.setDate(new java.util.Date(System.currentTimeMillis()));
}
Then I'd like to use this code, but instead of the long date "Thu Oct 07 00:00:00 GMT+02:00 2010" I would like to just see what month has been selected, like "October" or "10" etc.
String month = dateField.getDate().toString();
System.out.println("Selected month: " + month);
I can't find anything good on this dateField thing... I want to get the short date like "2010-10-05" if that specific date has been selected in the dateField.

Calendar cal= Calendar.getInstance();
cal.setTime(dateField.getDate());
String date = cal.get(Calendar.YEAR) + "-" + ( cal.get(Calendar.MONTH) + 1 ) + "-" + cal.get(Calendar.DAY_OF_MONTH);

Related

Creating an letter/number ID from the date using excel VBA

I am new to Excel VBA
Lets say I entered the date: 2022/05/31 I would like to generate AE31
Where A represents the year,
E represents the month and
31 represents the date
How could I generate this ID data in excel using VBA?
That could be:
TheDate = DateSerial(2022, 05, 31)
ID = Chr(Year(TheDate) - 2022 + 65) & Chr(Month(TheDate) - 1 + 65) & CStr(Day(TheDate))
ID -> AE31

groovy date plus giving wrong dates

I am trying to generate a random date and then add 5 number of days to it. it works fine most of the times but when the new date goes into next year, the result is actually going back to the beginning of the same year. Not sure why.. here is my code
Date today = new Date();
def endRange = 1500
def randomInterval = new Random().nextInt(endRange)
startDate = today.plus(randomInterval)
endDate= startDate.plus(5)
I run this in loop for 1000 times and 18 out of those are either resetting back to the same year or adding one year. not sure why. Below are two example output results
startdate enddate
2022-12-27 -- > 2022-01-01
2020-12-26 -- > 2021-12-31
appreciate any help on this
// update
after a bit digging, this is happening after I change the date format to "YYYY-MM-dd" before writing to excel as that is the format I need. and it is consistently happening to any date that gets rolled over to next year after adding 5 days.
Date today = new Date();
def minStart = today+5 //diff_currentDt_startDt
def endRange = 1500
def randomInterval = new Random().nextInt(endRange)
startDate = today.plus(randomInterval)
endDate= startDate.plus(diff_startDt_endDt)
// after this I format them and then write to excel
def startDate1=startDate.format('YYYY-MM-dd')
def endDate1=endDate.format('YYYY-MM-dd')
Label label1= new Label(0, i, startDate1)
sheet.addCell(label1)
Label label2= new Label(1, i, endDate1);
sheet.addCell(label2);
again checking further, somehow the format is messing it up. Below are the dates before and after formatting
Tue Dec 24 19:31:02 EST 2019 //startdate
2019-12-24 // startdate in YYYY-MM-dd format
Sun Dec 29 19:31:02 EST 2019 // enddate (startdate +5)
2020-12-29 // enddate in YYYY-MM-dd format
what am I doing wrong?
looks like the "YYYY" in .format('YYYY-MM-dd') is causing issue its fixed after changing it to "yyyy" in .format('yyyy-MM-dd')

Convert Date to Fiscal Period with C#

Is there a standard or convenient way to get the current financial period in a custom workflow activity and use it to convert a calendar date field to it's financial date?
Fiscal periods are monthly.
Manually removing months from the date field to get the fiscal date is not really an option.
I have found this set, but it is empty:
XRMServices/2011/OrganizationData.svc/MonthlyFiscalCalendarSet
I found this
<d:FiscalCalendarStart m:type="Edm.DateTime">
2013-10-01T10:59:00Z
</d:FiscalCalendarStart>
in
/XRMServices/2011/OrganizationData.svc/OrganizationSet
This method returns the fiscal period (as month) from a calendar date:
/* returns month, year */
private Tuple<int, int> GetFinancialDate(WorkflowContext context, DateTime? date)
{
var start = context.Linq.OrganizationSet
.FirstOrDefault().FiscalCalendarStart;
if (date.HasValue && start.HasValue)
{
var localDate = date.Value.ToLocalTime();
var month = localDate.Month - start.Value.ToLocalTime().Month + 1; // +1 because not 0 based
var year = (localDate.Month == 1) ? localDate.Year : localDate.Year + 1; // if it is Jan then do not move the year forward - move forward for Feb onward
if (month <= 0) // the date is in the previous financial year, 0 would be the last period of the previous fiscal year
{
--year; // move to the previous year
month = month + 12; // month is negative or 0 before this, so is 12 or less after
}
return Tuple.Create(month, year);
}
return Tuple.Create(0, 0);
}

How to get a date for 2 days before today in LotusScript?

I'm doing an agent to send late in notification for 2 days before today with Sunday is a non-working day. Thus if today is Monday, agent will send late attendance for last Friday, if Tuesday, it will send last Saturday, if Wednesday, it will send Monday and etc. I already know how to get today's date. How do I get 2 days before today?
Dim tdy, dayToUse
Dim intl As NotesInternational
Set intl = ss.International
tdy=intl.Today
You can do the following simple operation to get a specific date:
dayToUse = DateNumber( Year( Now ), Month( Now ), Day( Now ) - 2 )
You can also use the NotesDateTime object:
Dim dateTime As New NotesDateTime( "Today" )
Call dateTime.AdjustDay( -2 )
Hopefully this can help you.
Building on #Per Henrik Lausten's response:
In order to exclude Sunday, you can use the Weekday function. It's a builtin function of the LotusScript language, not a method of the NotesDateTime class. It takes a LotusScript date as its input, not a NotesDateTime. So you might want to do something like this:
Dim dateTime As New NotesDateTime( "Today" )
Dim day as variant
dim adjustment as integer
day = CDat(dateTime.dateOnly)
if Weekday(day) = 2 or Weekday(day) = 3 then ' Monday or Tuesday
adjustment = -3
else
adjustment = -2 ' any other day
end if
Call dateTime.AdjustDay( adjustment )
The above code has no special case for Today = Sunday. It's unclear to me whether you can just ignore that case or do something special.

Week number of a year in dateformat

I need to get the year I use the format YYYY, and month MM, and so on. I am looking to see if there is a format to get the week number of the year.
The date that I am using as example is 2008-03-09 16:05:07.000, and I would like to know the week number which is 11 in this case. Is there a way to retrieve 11 programmatically?
There's no format that provides the weeknumber, but you can get it in this way easily:
System.Globalization.CultureInfo cul = System.Globalization.CultureInfo.CurrentCulture;
int weekNum = cul.Calendar.GetWeekOfYear(
DateTime.Now,
System.Globalization.CalendarWeekRule.FirstFourDayWeek,
DayOfWeek.Monday);
CultureInfo.Calendar Property
Calendar.GetWeekOfYear Method
Custom Date and Time Format Strings
To answer your comment how to get the quarter of a year of a given DateTime:
int quarter = (int)((DateTime.Now.Month - 1) / 3) + 1

Resources