Asked  6 Months ago    Answers:  5   Viewed   32 times

The difficulty is that it should be cross platform. Windows 2000, XP, Vista, OSX, Linux, other unix variants. I am looking for a snippet of code that can accomplish this for all platforms, and a way to detect the platform.

Now, you should be aware of bug 4787931 that user.home does not work correctly, so please do not provide me of textbook answers, I can find these myself in the manuals.

 Answers

64

The bug you reference (bug 4787391) has been fixed in Java 8. Even if you are using an older version of Java, the System.getProperty("user.home") approach is probably still the best. The user.home approach seems to work in a very large number of cases. A 100% bulletproof solution on Windows is hard, because Windows has a shifting concept of what the home directory means.

If user.home isn't good enough for you I would suggest choosing a definition of home directory for windows and using it, getting the appropriate environment variable with System.getenv(String).

Tuesday, June 1, 2021
 
ritch
answered 6 Months ago
86

function daysInMonth (month, year) { // Use 1 for January, 2 for February, etc.
  return new Date(year, month, 0).getDate();
}

console.log(daysInMonth(2, 1999)); // February in a non-leap year.
console.log(daysInMonth(2, 2000)); // February in a leap year.

Day 0 is the last day in the previous month. Because the month constructor is 0-based, this works nicely. A bit of a hack, but that's basically what you're doing by subtracting 32.

Tuesday, June 1, 2021
 
danjah
answered 6 Months ago
87

I would agree with your approach to override hashCode() and equals() and use something that implements Set.

Doing so also makes it absolutely clear to any other developers that the non-duplicate characteristic is required.

Another reason - you get to choose an implementation that meets your needs best now:

  • HashSet
  • TreeSet
  • LinkedHashSet

and you don't have to change your code to change the implementation in the future.

Friday, June 11, 2021
 
godot
answered 6 Months ago
39

The File.expand_path method uses the Unix convention of treating the tilde (~) specially, so that ~ refers to the current user's home directory and ~foo refers to foo's home directory.

I don't know if there's a better or more idiomatic way, but File.expand_path('~') should get you going.

Friday, September 10, 2021
 
PLPeeters
answered 3 Months ago
100

This is a non-standard IE-only behavior and should not be used.

[1] [2] [3]

Tuesday, November 16, 2021
 
edorian
answered 2 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share