Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,471 members, 7,808,696 topics. Date: Thursday, 25 April 2024 at 03:33 PM

Convert This Java To Dotnet Very Urgent - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Convert This Java To Dotnet Very Urgent (2129 Views)

I Want To Develop A Software In Java To Calculate CGPA / What Topics To Cover In Java To Be A Guru? / Does Anyone Have Tips On Using Java To Interact With A Usb Device? Its For My Final Year Project. Pl (2) (3) (4)

(1) (Reply) (Go Down)

Convert This Java To Dotnet Very Urgent by luckyCO(m): 5:12pm On Jan 14, 2011
public static String toDate(Date d)
{
Calendar calendar = Calendar.getInstance();
calendar.setTime(d);
calendar.add(Calendar.MONTH, 1);
calendar.add(Calendar.DAY_OF_MONTH, 1);
String fieldDate = calendar.get(Calendar.DAY_OF_MONTH) + "/" + (calendar.get(Calendar.MONTH)) + "/" + calendar.get(Calendar.YEAR);

return fieldDate;
}

public static String toDate(long d)
{
Date date = new Date(d);

return Resources.toDate(date);
}
Re: Convert This Java To Dotnet Very Urgent by dazz4me: 10:11pm On Jan 14, 2011
I have never read or write java before so i don't know if i'm correct. if ok you can easily convert to C#

Public Shared Function toDate(ByVal d As Date) As String
            Dim fieldDate As String = ""
           d.AddMonths(1)
            d.AddDays(1)
            fieldDate = d.ToString("d")
            Return fieldDate
End Function
Public Shared Function toDate(ByVal d As Long) As String

            Dim _date As Date = New Date(d)

            Return toDate(_date)
End Function
Re: Convert This Java To Dotnet Very Urgent by luckyCO(m): 2:10am On Jan 15, 2011
I even did more than that and converted exactly how java did but was giving me another thing

Look at the code below

import java.util.Calendar;
import java.util.Date;

public class NewClass {
public static String toDate(Date d)
{
Calendar calendar = Calendar.getInstance();
calendar.setTime(d);
calendar.add(Calendar.MONTH, 1);
calendar.add(Calendar.DAY_OF_MONTH, 1);
String fieldDate = calendar.get(Calendar.DAY_OF_MONTH) + "/" + (calendar.get(Calendar.MONTH)) + "/" + calendar.get(Calendar.YEAR);

return fieldDate;
}

public static String toDate(long d)
{
Date date = new Date(d);

return NewClass.toDate(date);
}

}

I consume the class

long mydate=1149617771796l;

Date d = new Date(mydate);

System.out.println("today = " + d);

I got "today = Tue Jun 06 18:16:11 GMT 2006"

In dotnet I did the same thing as below



private function toDate( d as long) as string
Dim sdate As New Date(d)
return sdate
end function


I Consume it with
dim mydate as long =1149617771796l;
console.writeln todate(mydate ))

I got "1/2/1"

Why
Re: Convert This Java To Dotnet Very Urgent by Mobinga: 11:50am On Jan 15, 2011
dazz4me:

I have never read or write java before so i don't know if i'm correct. if ok you can easily convert to C#

Public Shared Function toDate(ByVal d As Date) As String
            Dim fieldDate As String = ""
           d.AddMonths(1)
            d.AddDays(1)
            fieldDate = d.ToString("d"wink
            Return fieldDate
End Function
Public Shared Function toDate(ByVal d As Long) As String

            Dim _date As Date = New Date(d)

            Return toDate(_date)
End Function

Shame on you. angry
Re: Convert This Java To Dotnet Very Urgent by Beaf: 6:47am On Jan 16, 2011
I've simply typed in all of this, so there might be a couple of syntax errors (I doubt it though). I've given three ways to do it, from the worst to the best. Enjoy.

1. This is a direct C# translation of your java code that will do what you want, but is the wrong way to write it.

using System;

public class NewClass
{
public static String ToDate(Datetime d)
{
return d.Day.ToString() + "/" + d.Month.ToString() + "/" + d.Year.ToString() ;
}

public static String ToDate(long d)
{
DateTime date = new DateTime(d);
return NewClass.ToDate(date);
}
}


2. This is a better way (not the best though):

using System;

public class NewClass
{
   public static String ToDate(Datetime d)
   {
      return d.ToShortDateString() ;
   }

   public static String ToDate(long d)
   {
      DateTime date = new DateTime(d);
      return NewClass.ToDatete(date);
   }
}


3. If you find yourself getting hammered with the wrong dates, its likely that you are being hit by globalisation / localisation issues; the system running the code is running on could be in a different time zone to what you expect, or it could be in a different locale. This is the proper way to kill that scenario.

using System;
using System.Globalisation;

public class NewClass
{
   public static String ToDate(Datetime d)
   {
      //convert d to universal time
      d = d.ToUniversalTime();
      // Sets the CurrentCulture British English English.
      Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB"wink;
      //variable to hold offset from GMT
      int offsetSpan = -5; //this is for a place that is 5 hours behind GMT
      DateTimeOffset offsetDate = new DateTimeOffset(d, new TimeSpan(offsetSpan, 0, 0));
      //format the string using standard date string formater
      return offsetDate.ToString("d"wink ;
   }

   public static String ToDate(long d)
   {
      DateTime date = new DateTime(d);
      return NewClass.ToDate(date);
   }
}

(1) (Reply)

[Advice] Please How do I become a Programmer? / How To Change Your Facebook Display Name / Meet The 19-year-old Who Built A New Search Engine To Rival Google And Youtube

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 16
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.