C# has Datetime.FromBinary(long) method which accepts long. I have long data = -8587633342590756227.
Datetime.FromBinary(-8587633342590756227) which gives {7/30/2015 10:10:26 AM}. How to convert it to date-time format in Java?
Thanks
The value .Net (de-)serializes via To-/FromBinary seems very specific to .Net, so I don't think there's an easy way to convert that value into a java.util.Date.
The easiest way I can think of from the perspective of a Java developer would be to convert your .Net DateTime to Unix time (see DateTimeOffset.ToUnixTimeMilliseconds) and then use that value for the java.util.Date constructor that accepts that value.
Related
I am aware of
NumberFormat nf = NumberFormat.getInstance(Locale.getDefault());
But I want all the numbers shown in my app to be formatted according to the locale, thus I don't think it will be a good way to format them one by one using the above method.
So is there some global setting/variable/configuration that I have to change in order to do that?
Locale-aware formatting requires more than just translating e.g. month names from one language to another. In Java that's handled by separate classes apart from the ones that actually hold the values, e.g. NumberFormat, DateFormat. So there's no way around using them like you already do.
What you could try is to create some wrappers or convenience methods (like formatDate(Date)) to simplify things for you. Also put format strings into Android Resources (res/values).
When converting Java to C#, what is the proper way to translate Locale to CultureInfo?
I know in C# we have CultureInfo.CurrentCulture for the current thread's culture, and CultureInfo.InvariantCulture for cases where we want to provide consistency for the sake of writing to files. But what do the Java Locale.ROOT and Locale.getDefault() mean, and how do they generally map to the CultureInfo options in C#?
Well, thanks to Andreas, I can surmize from the javadoc:
Locale.ROOT (Java) == CultureInfo.InvariantCulture (C#)
And
Locale.getDefault() (Java) == CultureInfo.CurrentCulture (C#)
I just wish that document was easier to find! I had Googled things like java locale.root equivalent c# and java locale.getdefault equivalent c# with no useful results.
I have an old legacy VB6 application that is sending me a date value as a double via a CDbl(Now) type statement. On the java side, the current code reads in the value as new Date(Long.parse(value)).
I cannot modify the java side, but can change the VB6 code. How would I go about getting the correct floating point value for Long.parse to parse the value correct with the date and time.
I think that the java dates start at 0 on some arbitrary date counting up from there (1/1/1970 I think), the VB 6 date would probably be the same type of idea, but I'm finding it difficult to track down that information about the VB6 data types. Presumably if I knew what the difference between the dates are, and the units of measurement were the same I could just offset the value I send to the java application.
One thing that you can try is calling VariantTimeToSystemTime on the VB6 side and package the resulting SYSTEMTIME structure into a Double, following the date/time encoding rules of Java. (I don't know those rules, so I assume that they're different from the OLE date/time encoding rules. If they're the same, you can just pass the Double directly.)
If you need to directly access the bytes of the date value in VB6, you can call VarPtr() - this gives you the address of the variable and you can read the bytes as you need. It may be safer than calling CDbl(). (I don't have VB6 installed at the moment but conversion functions may perform funny tricks on the input values. Calling VariantTimeToSystemTime avoids the need for CDbl().)
The string at the bottom of this post is the serialization of a java.util.GregorianCalendar object in Java. I am hoping to parse it in Python.
I figured I could approach this problem with a combination of regexps and key=val splitting, i.e. something along the lines of:
text_inside_brackets = re.search(r"\[(.*)\]", text).group(1)
and
import parse
for x in [parse('{key} = {value}', x) for x in text_inside_brackets.split('=')]:
my_dict[x['key']] = x['value']
My question is: What would be a more principled / robust approach to do this? Are there any Python parsers for serialized Java objects that I could use for this problem? (do such things exist?). What other alternatives do I have?
My hope is to ultimately parse this in JSON or nested Python dictionaries, so that I can manipulate it it any way I want.
Note: I would prefer to avoid a solution relies on Py4J mostly because it requires setting up a server and a client, and I am hoping to do this within a single
Python script.
java.util.GregorianCalendar[time=1413172803113,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="America/New_York",offset=-18000000,dstSavings=3600000,useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=America/New_York,offset=-18000000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2014,MONTH=9,WEEK_OF_YEAR=42,WEEK_OF_MONTH=3,DAY_OF_MONTH=13,DAY_OF_YEAR=286,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=2,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=3,MILLISECOND=113,ZONE_OFFSET=-18000000,DST_OFFSET=3600000]
The serialized form of a GregorianCalendar object contains quite a lot of redundancy. In fact, there are only two fields that matter, if you want to reconstitute it:
the time
the timezone
There is code for extracting this in How to convert Gregorian string to Gregorian Calendar?
If you want a more principled and robust approach, I echo mbatchkarov's suggestion to use JSON.
I have a slight problem with my JSON string which I have converted from ASP.NET.
The JSON string that C# has converted for me looks like this: /Date(1338199919727)\/
I guess it converts it into milliseconds or something, but how do I get a Java-date out of this huge integer?
I need to get the date displayed properly on my Android app (as a string in a specific format (dd-MM-yyyy : HH), that's why I need it to be in Java. Do I have to manually make some form of converter myself, and figure out how to do it? Or is there some sweet, easy way of doing this?
but how do i get a java-date out of this huge integer?
Ah, that part's easy:
Date dt = new Date(1338199919727L);
You are quite right, it's milliseconds-since-The-Epoch (Jan 1st at midnight, 1970). And Java's Date object has a constructor that accepts that very value.
In general, most JSON parsers have the concept of a "reviver" function or class that you can pass to them, which will let you pre-process values as the JSON is being deserialized. I don't know Android's JSON parser well enough to know if it has one. If not (which would be a bit backward), you'd have to walk the resulting object graph looking for properties that had become strings and converting them to dates after-the fact.
I hope this is what you need
long Ldate = 1338199919727L;
Date date = new Date(Ldate * 1000);
String strdate = (String) DateFormat.format("MM/dd/yy h:mmaa", date);