How to convert DateTime value into Filename in J2ME - java-me

I am doing a Java mobile program using J2ME.
I want to save date time field value into a Mysql, I am trying to convert the datetime field into a long and saving it in Mysql if its possible.
In c# ToFileTime() method which does it, is there anything like that in J2ME??
public DateField getDateField() {
if (dateField == null) {
// write pre-init user code here
dateField = new DateField("dateField", DateField.DATE_TIME);
dateField.setDate(new java.util.Date(System.currentTimeMillis()));
// write post-init user code here
long myFileTime = dateField.
}
return dateField;
}

you need to correct 2 lines
//no need to pass system current time. new Date() creates current time date
dateField.setDate(new java.util.Date());
// gettime returns time in millis
long myFileTime = dateField.getDate().getTime();

Related

Converting String to datefield / Java Microedition

Using J2ME, netbeans 7.2, Developing a mobile app..
I have converted the Datefield value to a String and Now want to put it back to a Datefield. To do this I need to convert the String back to Datefield, I am using the following code but its not happening.
long myFileTime = dateField.getDate().getTime(); // getting current/set date from the datefield into long
String date = String.valueOf(myFileTime); // converting it to a String to put it back into a different datefield
Date updatedate= stringToDate(date); // passing the string 'date' to the Method stringToDate() to convert it back to date.
dateField1.setDate(updatedate); // updating the date into the new datefield1
public Date stringToDate(String s)
{
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(s.substring(0, 2)));
c.set(Calendar.MONTH, Integer.parseInt(s.substring(3, 5)) - 1);
c.set(Calendar.YEAR, Integer.parseInt(s.substring(6, 10)));
return c.getTime();
}
Since you have mentioned that you have the long myFileTime around, you should be able to use:
Date updatedate=new Date(myFileTime);
To convert back to your date. If only your String is available, you should modify your function to this:
public Date stringToDate(String s){
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(s.substring(0, 2)));
c.set(Calendar.MONTH, Integer.parseInt(s.substring(2, 4))-1 );
c.set(Calendar.YEAR, Integer.parseInt(s.substring(4, 8)));
return c.getTime();
}
Note the changed indexes.
In Java SE, you should be able to use the following line, instead of setting each fields separately:
c.setTimeInMillis(Long.parseLong(s));
Since in s you have the dateField.getDate().getTime() that is equal to myFileTime, the number of seconds starting with January 1, 1970, based on your provided code.
Your stringToDate should work only if your string will have the following format: ddMMyyyy. Also note that in this case you should use a SimpleDateFormat to parse, something like:
Date updatedate = new java.text.SimpleDateFormat("ddMMyyyy HH:mm:ss").parse(date);

Entry element for mm:ss with MonoTouch.Dialog?

I am giving a try to MonoTouch and MonoTouch.Dialog and was wondering what would be the best way to create a Time entry element for minutes and seconds only. The TimeElement only seems to support hours and minutes.
I am very new to the whole framework, so I am wondering if there a way to create a text entry element with sort of a "##:##" mask and use a numeric keypad to populate the minutes and seconds?
A cooler option would be to use a section that would take the user to a view with two "Picker Views" (rotating wheels) for minutes and seconds, but I am not there yet.
Thanks in advance
MonoTouch.Dialog's source code is available on Github (https://github.com/migueldeicaza/MonoTouch.Dialog/tree/master/MonoTouch.Dialog).
There, have a look at the implementation of the DateElement (https://github.com/migueldeicaza/MonoTouch.Dialog/blob/master/MonoTouch.Dialog/Elements.cs#L1827)
To get you a TimeElementMinutesSeconds, all you have to do is (untested) abuse the count down mode. It will offer hours and minutes, but you can just interpret them as minutes and seconds:
public class TimeElementMinutesSeconds : DateTimeElement {
public TimeElementMinutesSeconds (string caption, DateTime date) : base (caption, date)
{
}
public override string FormatDate (DateTime dt)
{
DateTime dtLocal = GetDateWithKind (dt).ToLocalTime ();
return string.Format("{0:##}:{1:##}"dtLocal.Hours, dtLocal.Minutes);
}
public override UIDatePicker CreatePicker ()
{
var picker = base.CreatePicker ();
picker.Mode = UIDatePickerMode.CountDownTimer;
return picker;
}
}
If that doesn't get you near enough, you can create your own picker and return that instead.

SharePoint 2007 SPListItem.File.TimeLastModified returned wrong date

In SharePoint 2007, when I saved a document to a document library, the value shown in the list view under Modified is:
18/6/2012 13:06
But yet when I programmatically access the field which I assume is SPListItem.File.TimeLastModified, it returned:
18/6/2012 3:06:43 AM
That means the 1 has been truncated, and so whatever that is reading the value turned 1pm into 3am.
The site and the webs are all inheriting the correct time zone. What should I do to make TimeLastModified to display the correct time? Or is this possible at all?
Thanks.
The TimeLastModified property returns the value always in UTC. Date/time values displayed on SP pages are usually converted to the time zone according to the culture of the current user. It is a good practice to show everything in the user's time zone but internally save the value in UTC.
If you want to convert the UTC value to the current user's time zone and then maybe print it in UI, you can use the following code:
SPFile file = ...;
SPWeb web = ...; // SPContext.Current.Web or file.Item.ParentList.ParentWeb or ...
DateTime time = UTCToWebTime(file.TimeLastModified, web);
string text = FormatWebTime(time, web);
DateTime UTCToWebTime(DateTime utcTime, SPWeb web) {
SPTimeZone timeZone = web.RegionalSettings.TimeZone;
DateTime localTime = timeZone.UTCToLocalTime(utcTime);
return DateTime.SpecifyKind(localTime, DateTimeKind.Local);
}
// Uses SPRegionalSettings to be more accurate then value.ToString(web.Locale).
string FormatWebTime(DateTime value, SPWeb web) {
SPRegionalSettings regionalSettings = web.RegionalSettings;
DateOptions dateOptions = new DateOptions(
regionalSettings.LocaleId.ToString(CultureInfo.InvariantCulture),
(SPCalendarType) regionalSettings.CalendarType, null,
regionalSettings.FirstDayOfWeek.ToString(CultureInfo.InvariantCulture),
regionalSettings.AdjustHijriDays.ToString(CultureInfo.InvariantCulture),
null, null));
string timePattern = regionalSettings.Time24 ?
dateOptions.TimePattern24Hour : dateOptions.TimePattern12Hour;
DateTimeFormatInfo format = web.Locale.DateTimeFormat;
return value.ToString(format.ShortDatePattern, format) + " " +
value.ToString(timePattern, format);
}
--- Ferda

convert strings to datetime

I have asp.net application and am using Entity Framework to connect it with the database. in this application, I have a textbox to get the date(am using calender css style here), and its in the string type.
I have a column in my database and its in Date Time format, I need to compare the textbox value with the date column in my database, for this I just used the code as
public StudentAttendances(string date)
{
if (date != "")
{
DateTime date1 = Convert.ToDateTime(date);
foreach (DataAccess.StudentAttendance studentAttendance in buDataEntities.StudentAttendances.Where(s => s.Date == date1))
{
this.Add(new StudentAttendance(studentAttendance.StudentId));
}
}
}
for example if I select a date in my textbox(the format is 04/05/2012) and when I compare this with the database its not showing any data, but actually some datas are there for this date.
Your code is comparing both day and time (hours, minutes etc will have to match). Try comparing just the day part like this:
buDataEntities.StudentAttendances.Where(s => s.Date.Subtract(date1).Days == 0)
I also think that you should specify what format the input date from the users is in.
04/05/2012 may mean both 4th April or 5th of May depending on your computers regional setting.
Here is an example (below) for converting a date string in American format to DateTime object:
DateTime date1 = DateTime.Parse(date, new CultureInfo("en-US"));
Hope that helps!
your ask is very limited, but try to see this
http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html
Please check whether the following works for you:
public StudentAttendances(string date)
{
if (date != "")
{
// please see the change from your given code
DateTime date1 = DateTime.ParseExact(date, "MM/dd/yyyy",
System.Globalization.CultureInfo.InvariantCulture);
foreach (DataAccess.StudentAttendance studentAttendance in buDataEntities.StudentAttendances.Where(s => s.Date == date1))
{
this.Add(new StudentAttendance(studentAttendance.StudentId));
}
}
}

MOSS 2007: SPListItem.GetFormattedValue for DateTime fields has a bug?

SPListItem.GetFormattedValue seems to have a strange behavior for DateTime fields.
It retrieves the DateTime value through SPListItem's indexer which according to this MSDN article returns local time.
Here's a snippet from Reflector
public string GetFormattedValue(string fieldName)
{
SPField field = this.Fields.GetField(fieldName);
if (field != null)
{
return field.GetFieldValueAsHtml(this[fieldName]);
}
return null;
}
So it uses SPListItem's indexer to retrieve the value and than SPFields.GetFieldValueAsHtml to format the value. GetFieldValueAsHtml seems to assume the date is in UTC and convert it to local time no matter what kind it is. (Reflector shows that it uses GetFieldValueAsText which uses value.ToString() but for some reason it assumes the time to be UTC.)
The end result is that the string representation on a time field obtained trough listItem.GetFormattedValue() (at least in my case) is incorrect, being local time + (local time - UTC).
Have anybody encountered the same issue with SPListItem.GetFormattedValue() and what was your workaround?
Converting the date back to universal time before calling GetFieldValueAsHtml works just fine.
DateTime localTime = (DateTime)item["DueDate"];
// this is local time but if you do localDateTime.Kind it returns Unspecified
// treats the date as universal time..
// let's give it the universal time :)
DateTime universalTime = SPContext.Current.Web
.RegionalSettings.TimeZone.LocalTimeToUTC(localTime);
string correctFormattedValue =
item.Fields["DueDate"].GetFieldValueAsHtml(universalTime);
I have had a recognised bug with the date conversion from UTC in SharePoint. It was fixed in SP1.

Resources