How to create a field of format dd/yyyy? - java-me

The field is like a DateField but instead its value is just the month and the year like '05/2011' : how to create such a field ?

In java-me, you can use the java.util.Calendar package for formatting the date.
Here is snippet from this tutorial on displaying the date and time in Java ME:
private void outputDateUsingCalendar(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
StringBuffer sb = new StringBuffer();
int day = calendar.get(Calendar.DAY_OF_MONTH);
sb.append(numberToString(day));
sb.append("-");
int month = calendar.get(Calendar.MONTH) + 1;
sb.append(numberToString(month));
sb.append("-");
sb.append(calendar.get(Calendar.YEAR));
StringItem item = new StringItem("Using Calendar", sb.toString());
mainForm.append(item);
}

Related

Flutter/Dart: Excel Date to Date Object

I am parsing an excel document in excel using the excel: ^1.1.5 package. In my sheet, i have a Date column and this Date is being received in my Flutter code in the following format
"44663"
rather than:
"2022/04/12"
How do I parse this to a format such as YY-MM-DD.
I have tried DateTime.parse(), but it throws an error that my date format is invalid.
I found the answer :
const gsDateBase = 2209161600 / 86400;
const gsDateFactor = 86400000;
final date = double.tryParse("44663");
if (date == null) return null;
final millis = (date - gsDateBase) * gsDateFactor;
print(DateTime.fromMillisecondsSinceEpoch(millis.toInt(), isUtc: true));

How do I read cell value from Excel sheet based on the cell attribute (keyword) using Apache POI integration for Selenium

Example of Excel Data
I need to read the above Example Data using cell reference attribute (highlighted in blue)
The table in the sheet is maintained in column order.
For example, if the table is something like:
firstName
Nick
Jack
lastName
Fury
Ryan
personalEmail
nick-fury#example.com
jack-ryan#example.com
Then I want the script to run for:
firstName
Nick
lastName
Fury
personalEmail
nick-fury#example.com
And then run for:
firstName
Jack
lastName
Ryan
personalEmail
jack-ryan#example.com
And be accessible using the corresponding attributes (firstName, lastName, personalEmail) in my code for the ExcelReader class.
Here's what I want to know:
Is there a way to achieve this using Apache-poi extension for Java?
What function libraries can I used from the apache-poi extension?
What code should I use in my utilities package?
Thanks in Advance :)
To solve this you need to reverse the data getting logic.
So here we first need to get the column data and then traverse all its row.
ie. Nick -> Fury -> nick-fury#example.com and then moving to another column and fetch Jack -> Ryan -> jack-ryan#example.com
Screenshot:
Important Note:
This code is to fetch xls file data using POI, kindly change the code as
per your requirement.
(1). HSSFWorkbook: This class has methods to read
and write Microsoft Excel files in .xls format.
(2).XSSFWorkbook: This class has methods to read and write Microsoft
Excel and OpenOffice xml files in .xls or .xlsx format.
Code:
#Test(dataProvider = "getExcelData")
public void testSheet(String firstName, String lastName, String personalEmail) {
System.out.println(firstName+" "+lastName+" "+personalEmail);
}
#DataProvider
public Object[][] getExcelData(){
String excelSheetPath = System.getProperty("user.dir")+"/data.xls";
String sheetName = "Sheet1";
return getExcelData(excelSheetPath, sheetName);
}
public Object[][] getExcelData(String excelSheetPath, String sheetName) {
Object[][] arrayExcelData = null;
try (
FileInputStream fileStream = new FileInputStream(excelSheetPath)
) {
HSSFWorkbook workbook = new HSSFWorkbook(fileStream);
HSSFSheet sheet = workbook.getSheet(sheetName);
Row row = sheet.getRow(0);
int lastRowIndex = sheet.getLastRowNum() + 1;
System.out.println("Last row index :" + lastRowIndex);
int totalNoOfCols = row.getLastCellNum() - 1;
System.out.println("Total columns :" + totalNoOfCols);
arrayExcelData = new Object[totalNoOfCols][lastRowIndex];
DataFormatter df = new DataFormatter();
for (int i = 1; i <= totalNoOfCols ; i++) {
for (int j = 0; j < lastRowIndex; j++) {
row = sheet.getRow(j);
Cell c = row.getCell(i);
String cellData = df.formatCellValue(c);
System.out.println(cellData);
arrayExcelData[i-1][j] = cellData;
}
System.out.println("-----------");
}
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
return arrayExcelData;
}

Find start and end date of selected period in p:schedule

Requirement: I want to load some data from my Database based on week start date and end date. In Default, I am able to load the current week Data because from the current date I am able to get the week start date and end date.
Problem: When user click Next and Previous button of Schedule component how to get the start and end date of that week.
Just check the showcase for p:schedule. When a different period is selected, the ScheduleEvent#loadEvents(Date start, Date end) method will be called.
From the showcase, in ScheduleView.java:
#PostConstruct
public void init() {
eventModel = new DefaultScheduleModel();
// Some default events are added here
lazyEventModel = new LazyScheduleModel() {
#Override
public void loadEvents(Date start, Date end) {
Date random = getRandomDate(start);
addEvent(new DefaultScheduleEvent("Lazy Event 1", random, random));
random = getRandomDate(start);
addEvent(new DefaultScheduleEvent("Lazy Event 2", random, random));
}
};
}

DirectorySearcher.Filter which queries active directory to return upcoming events of the current week

I am trying to get all birthdays and anniversaries which fall under current week. I am using Directory Searcher and LDAP. I am new to LDAP and I am using the below code:
string _path = "LDAP:";
System.DirectoryServices.DirectoryEntry entry = new System.DirectoryServices.DirectoryEntry(_path);
DirectorySearcher ds = new DirectorySearcher(entry);
string month = DateTime.Now.Month.ToString();
string day = DateTime.Today.Day + numDays.ToString();
ds.Filter = "(&(objectClass=user)(description=" + month + "\\" + day +"))";
SortOption option = new SortOption("description", System.DirectoryServices.SortDirection.Ascending);
ds.Sort = option;
DataSet dSet = new DataSet();
DataTable dTable = new DataTable("Events");
dTable.Columns.Add("birthday");
foreach (System.DirectoryServices.SearchResult resEvent in ds.FindAll())
{
System.DirectoryServices.DirectoryEntry de1 = resEvent.GetDirectoryEntry();
DataRow dRow = dTable.NewRow();
if (de1.Properties["description"].Value != null)
{
dRow["birthday"] = de1.Properties["description"].Value.ToString();
dTable.Rows.Add(dRow);
}
}
dSet.Tables.Add(dTable);
return dSet;
Are your events stored under the user's description attribute?
Not much detailas to what your attribute values look like.
You are trying to user c# which I do not have access to, but from a LDAP Query, this works:
(&(objectClass=user)(description=09/15*))
-jim

String to date Format in j2me

I have a String Wed, 22 Aug 2012 06:29:31 +0530 like this, now I want to convert this String to a date format and I need to display day an date Wed, 22 Aug 2012 like this and eliminating other remaining String?
I need to display the date on my LWUIT form Screen
String date = "Wed, 22 Aug 2012 06:29:31 +0530";
String shortDate = date.substring(0, 16);
You need to write the code manually by using the methods in java.util.Calendar, we did some formatters and localization API's in Codename One so you can look at our code for reference or just move to Codename One.
For this date formate "date_posted":"2012-04-19T16:45:33+01:00" i use the following code. You can customize it for your needs.
try {
String dateString = date_posted;
String[] dateArray = Util.split(dateString, "T");
String[] date = Util.split(dateArray[0], "-");
String[] time = Util.split(dateArray[1], ":");
Calendar calStart = Calendar.getInstance();
calStart.set(Calendar.MONTH, Integer.parseInt(date[1]) - 1);
calStart.set(Calendar.DAY_OF_MONTH, Integer.parseInt(date[2]));
calStart.set(Calendar.YEAR, Integer.parseInt(date[0]));
calStart.set(Calendar.HOUR_OF_DAY, Integer.parseInt(time[0]));
calStart.set(Calendar.MINUTE, Integer.parseInt(time[1]));
calStart.set(Calendar.MILLISECOND, 0);
Date postDate = calStart.getTime();
post_time = postDate.getTime();
} catch (Exception ex) {
Logger.err("fromJSON", ex);
}
I use something like this :
String getDateString(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
return new String(/*Any format you need*/);
}
There is no specific method in j2me to convert string to date. So, that is not possible at all.

Resources