16

In Xamarin.Android, you work with both .NET and Java.

I get a return value of Java.Util.Date, I then need to input that same value as a parameter that only takes System.DateTime

This is how I currently do it

public static DateTime ConvertJavaDateToDateTime(Date date)
{
    var a = date.ToGMTString();
    var b = date.ToLocaleString();
    var c = date.ToString();
    DateTime datetime = DateTime.ParseExact(date.ToGMTString(), "dd MMM yyyy HH:mm:ss 'GMT'", CultureInfo.InvariantCulture);
    return datetime;
}

However on the first 9 days of any month, I only get 1 digit for the day, and the DateTime.ParseExact function is looking for dd (i.e. 2 digits for the day).

a is a string with value "1 Sep 2014 14:32:25 GMT"

b is a string with value "1 Sep 2014 16:32:25"

c is a string with value "Mon Sep 01 16:32:25 EET 2014"

I wish I could find a simple, quick, reliable and consistent solution for this problem :D

Mohamed Heiba
  • 1,813
  • 4
  • 38
  • 67

8 Answers8

16

java.util.Date has a getTime() method, which returns the date as a millisecond value. The value is the number of milliseconds since Jan. 1, 1970, midnight GMT.

With that knowledge, you can construct a System.DateTime, that matches this value like so:

public DateTime FromUnixTime(long unixTimeMillis)
{
    var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
    return epoch.AddMilliseconds(unixTimeMillis);
}

(method taken from this answer)

Community
  • 1
  • 1
Dennis
  • 2,119
  • 20
  • 29
1

Do this:

public DateTime ConvertDateToDateTime(Date date)
{
    SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd hh:mm:ss");
    String dateFormated = dateFormat.format(date);

   return new DateTime(dateFormated);
}
m4tt1mus
  • 1,642
  • 14
  • 24
Cícero Moura
  • 2,027
  • 1
  • 24
  • 36
0

I'll update my answer, since you've changed the question.

you can use a long to hold the milliseconds and than convert the milliseconds to ticks(x10000) and create a new DateTime

Date date = new Date();
Long milliseconds = date.getTime();
Long ticks = milliseconds * 10000
DateTime datetime = DateTime(ticks);
Native
  • 157
  • 12
  • I could but it that a reliable and consistent solution ? – Mohamed Heiba Jul 04 '14 at 13:14
  • thanks a lot, sounds very promising, let me try that – Mohamed Heiba Jul 04 '14 at 13:27
  • 1
    Ok it works well, but the year is wrong, it says year {01/09/0045 14:32:25} – Mohamed Heiba Jul 04 '14 at 13:36
  • That's odd. What is the date value that you are passing? also, you should check these out:[Ticks per millisecond](http://msdn.microsoft.com/en-us/library/system.timespan.tickspermillisecond.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2) and [DateTime](http://msdn.microsoft.com/en-us/library/z2xf7zzk(v=vs.110).aspx) – Native Jul 04 '14 at 13:48
0

I had this problem when authenticating with Facebook to receive the Expire time for the token. The solution was to do this:

var convertedTime = new DateTime(1970,1,1,0,0,0,DateTimeKind.Utc).AddMilliseconds(MyJavaUtil.Date.Time);
0

I used this:

DateTimeOffset.FromUnixTimeMilliseconds(date.Time - (date.TimezoneOffset * 60 * 1000)).DateTime;

To incorporate the timezone offset into my date.

Kes Walker
  • 1,154
  • 2
  • 10
  • 24
0

In my case, only this code works correctly:

public static DateTime NativeDateToDateTime(Java.Util.Date date)
{ 
    SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
    string dateFormated = dateFormat.Format(date);
    
    return DateTimeOffset.Parse(dateFormated, null, DateTimeStyles.None).DateTime; 
}
Polyariz
  • 534
  • 1
  • 7
  • 17
-1
It is not tested but try with Calendar methods

public static String  ConvertJavaDateToDateTime(Date date)
{
     Calendar c = new GregorianCalendar();
        c.setTime(date);        
        System.out.println(c.getTime());
      return c.getTime();
}

Prints:

Tue Aug 06 00:00:00 EDT 2013
Sandeep
  • 766
  • 5
  • 16
-2
You can try with this also

public DateTime dateAndTimeToDateTime(java.sql.Date date, java.sql.Time time) {
    String myDate = date + " " + time;
    SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy HH:mm:ss 'GMT'");
    java.util.Date utilDate = new java.util.Date();
    try {
        utilDate = sdf.parse(myDate);
    } catch (ParseException pe){
        pe.printStackTrace();
    }
    DateTime dateTime = new DateTime(utilDate);

    return dateTime;
}
Sandeep
  • 766
  • 5
  • 16