Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
I'm attempting to use the Jersey REST client to interact with a third-party REST API. I'm a bit confused on how to write my POJOs though.

For example, there is an API call that returns something like:
code:
<AccountsList xmlns="asdf">
  <Account>
    <Name>Joe Blow</Name>
  </Account>
</AccountsList>
So I assume I have to write two POJOs, an AccountsList and an Account. I'm trying with just empty classes right now, no member variables or anything yet, but I keep running into "unexpected element" errors. Can somebody point me in the right direction?

edit: getting closer I think...

code:
public static void main(String[] args) {
  Client c = Client.create();
  WebResource r = c.resource("https://some-third-party-url.com/AccountsList");
  List<Account> accounts = r.accept(MediaType.APPLICATION_XML_TYPE).get(new GenericType<List<Account>>(){});
  for (Account account : accounts) {
    System.out.println(account.getName());
  }
}
code:
@XmlRootElement(name = "Account", namespace="http://some-third-party-url.com/api/whatever")
public class Account {
  private String name;
  @XmlElement(name = "Name")
  public String getReportName() {
    return name;
  }
  public void setReportName() {
    this.name = name;
  }
}
account.getName() is null though

fletcher fucked around with this message at 22:20 on Nov 6, 2012

Adbot
ADBOT LOVES YOU

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
Ahh, it works correctly if I add the same namespace to that @XmlElement annotation. Is there a way to avoid having to do that every single time?

1337JiveTurkey
Feb 17, 2005

I believe that you want the @XmlSchema annotation on your package.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
I've added a package-info.java file to the same folder as Account.java:

code:
@XmlSchema(elementFormDefault = XmlNsForm.UNQUALIFIED, namespace="http://some-third-party-url.com/api/whatever")
package com.my.package;

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
But it doesn't seem to work correctly...

1337JiveTurkey
Feb 17, 2005

I haven't really messed around with the JAXB namespace stuff, but maybe you want the elementFormDefault to be QUALIFIED. UNQUALIFIED would mean that you don't want elements to be part of a namespace by default. You'd probably still want to leave the attribute default to UNQUALIFIED or UNSET since normally nobody namespaces them.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
Yep, that did it! Thank you. Now that I've got my little prototype working with Jersey, time to try the same thing with CXF.

Joda
Apr 24, 2010

When I'm off, I just like to really let go and have fun, y'know?

Fun Shoe
Lately, we've been using StdDraw a lot for programming hand-ins, and whilst I don't have a choice currently, I was still wondering if there are similar libraries that aren't so loving infuriating? It just seems extremely counter-intuitive to make everything based around floats for a draw library, and there has to be some similarly easy-to-implement library that uses traditional coordinates.

Joda fucked around with this message at 17:38 on Nov 7, 2012

Houston Rockets
Apr 15, 2006

I'm running a RESTful Jersey app with EJB 3.1 beans on a Glassfish 3.1.2 server. I have two databases (Vertica and MySQL).

I keep getting this error -

code:
#|2012-11-06T01:34:30.032+0000|WARNING|glassfish3.1.2|javax.enterprise.system.container.ejb.com.sun.ejb.containers|_ThreadID=19;_ThreadName=Thread-2;|javax.ejb.TransactionRolledbackLocalException: Client's transaction aborted  
        at com.sun.ejb.containers.BaseContainer.useClientTx(BaseContainer.java:4722)  
        at com.sun.ejb.containers.BaseContainer.preInvokeTx(BaseContainer.java:4601)  
        at com.sun.ejb.containers.BaseContainer.preInvoke(BaseContainer.java:1914)  
        at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212)  
        at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)  
        at $Proxy349.getProperty(Unknown Source)  
        at com.chuckleberryfinn.platform.util.__EJB31_Generated__PropertiesBean__Intf____Bean__.getProperty(Unknown Source)
I am unable to reproduce it with any consistency. Sometimes it will happen after three calls to the app, sometimes after thousands. It always comes from different beans as well (so the last line of the stack trace will change but it always ends up going to BaseContainer.useClientTx).

I also see a lot of these:

code:
[#|2012-11-08T21:35:17.911+0000|SEVERE|glassfish3.1.2|com.locusenergy.platform.messages.LocusMessage|_ThreadID=19;_ThreadName=Thread-2;|javax.ejb.EJBTransactionRolledbackException  
javax.ejb.EJBTransactionRolledbackException  
        at com.sun.ejb.containers.BaseContainer.mapLocal3xException(BaseContainer.java:2314)  
        at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2096)  
        at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1994)  
        at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:222)  
        at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)
Any ideas?

Volguus
Mar 3, 2009
I assume you have a JTA transaction manager, right? Something must happen there that the TM that you're using decides to roll back things. are there no other exceptions in the log?

Houston Rockets
Apr 15, 2006

rhag posted:

I assume you have a JTA transaction manager, right? Something must happen there that the TM that you're using decides to roll back things. are there no other exceptions in the log?

Yes. No other exceptions in my server.log. Is there another log somewhere that holds transaction service logs?

Volguus
Mar 3, 2009
I am not familiar with glassfish, so I cannot help you there. The other application servers that I used didn't have a separate log for transactions.
The only way I can see right now to approach the problem is to log the heck out of your every move, every call that modifies the JPA entities so that you can have an idea what are you trying to save at that point in time.

OR...another idea i just had: use an aspect. An around aspect that you can apply selectively to certain methods (useClientTx or...any other) and that would log in case of exception what were the parameters passed that made it throw an exception. Maybe even get the main cause exception, in case in the log it gets eaten somehow (is it configurable? I assume it should be).

Without a proper stacktrace is very hard (sometimes impossible) to debug problems like this.

wins32767
Mar 16, 2007

Houston Rockets posted:

I'm running a RESTful Jersey app with EJB 3.1 beans on a Glassfish 3.1.2 server. I have two databases (Vertica and MySQL).

I keep getting this error -

code:
#|2012-11-06T01:34:30.032+0000|WARNING|glassfish3.1.2|javax.enterprise.system.container.ejb.com.sun.ejb.containers|_ThreadID=19;_ThreadName=Thread-2;|javax.ejb.TransactionRolledbackLocalException: Client's transaction aborted  
        at com.sun.ejb.containers.BaseContainer.useClientTx(BaseContainer.java:4722)  
        at com.sun.ejb.containers.BaseContainer.preInvokeTx(BaseContainer.java:4601)  
        at com.sun.ejb.containers.BaseContainer.preInvoke(BaseContainer.java:1914)  
        at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212)  
        at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)  
        at $Proxy349.getProperty(Unknown Source)  
        at com.chuckleberryfinn.platform.util.__EJB31_Generated__PropertiesBean__Intf____Bean__.getProperty(Unknown Source)
I am unable to reproduce it with any consistency. Sometimes it will happen after three calls to the app, sometimes after thousands. It always comes from different beans as well (so the last line of the stack trace will change but it always ends up going to BaseContainer.useClientTx).

I also see a lot of these:

code:
[#|2012-11-08T21:35:17.911+0000|SEVERE|glassfish3.1.2|com.locusenergy.platform.messages.LocusMessage|_ThreadID=19;_ThreadName=Thread-2;|javax.ejb.EJBTransactionRolledbackException  
javax.ejb.EJBTransactionRolledbackException  
        at com.sun.ejb.containers.BaseContainer.mapLocal3xException(BaseContainer.java:2314)  
        at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2096)  
        at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1994)  
        at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:222)  
        at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)
Any ideas?

I've run into a similar exception before but under very different circumstances. I never did fully diagnose it, but it was looking like it was due to some screwiness with connection pooling, Servlet member variables, and one database in the cluster going down.

Max Facetime
Apr 18, 2009

This stackoverflow question suggests that an exception can leave the transaction in an aborted state that triggers the rollback exception when it's next accessed. So I guess check all your catch-blocks to make sure every exception is logged so you don't miss the original cause.

Insane Totoro
Dec 5, 2005

Take cover!!!
That Totoro has an AR-15!
I'm currently working on using arrays right now. I am a little confused as to what exactly I am doing wrong with this particular homework question. Do I have a fundamental misunderstanding of how arrays work in Java? The index of the elements starts at "0" if I am not mistaken? Or can I just not return an array from a method the way I think I can in my program here?

Perhaps a better question, do I always have to declare the length of an array before I can write to its indexes?

Thanks, above questions answered!



The error message the compiler gives me is "java.lang.ArrayIndexOutOfBoundsException: 0"

Is this something where the elements of my arrays do not match somehow? Is something not writing to the correct index in an array?

The question is:

quote:

Use the method "public static int[] makeArray(int value)" to write a program where the method makeArray() tkaes the digits of the array value and stores them into an array. Then use the makeArray() function as part of a program that prints out the digits of the original number, backwards

So I did the following pseudocode:

//Have user enter in a number
//Call the method (below) and print the reversed array

//Create a method that takes the the user's number
//First count how many digits are in the number
//Create an array that has the same number of elements as digits in the user's number
//Store the digits of the user's integer into an array in reverse order
//Return from the method the reversed array



Java code:
public class reverseDigits
{
 public static void main(String[] args) 
  {
  
   Scanner input = new Scanner(System.in);
   
   int userChoice = 0;
   
   System.out.print("Input your array: "); 
   userChoice = input.nextInt();
   
   int[] reverseArray;
   reverseArray = new int [400];
   reverseArray = makeArray(userChoice);
   
   for (int i = 0; i <= reverseArray.length; i++){
   System.out.print(reverseArray[i]);
   }
   
   
 }
 
 public static int[] makeArray(int value) {
   
   int value2 = value;
   int counter = 0;
 
   for (int i = 0; i != 0; i++){
         value2 = value2 % 10;
         counter++;
    }
         
   int[] numArray;
   numArray = new int [ counter ];
   
   for (int i = 0; i <= numArray.length; i++){
     numArray[numArray.length - i] = value % 10; 
     value = value / 10;
   }
   
   return numArray;
 
  
 }

}
:siren: BIG EDIT

I fundamentally misunderstood the question. The method I should be using is "public static int[] makeArray(int value)" and not "public static int[] makeArray(int[] value)"

I have amended my pseudocode and code above to match this.

However I am still getting the error message "java.lang.ArrayIndexOutOfBoundsException: 0"

I did seem to have declared all the correct indexes?

Insane Totoro fucked around with this message at 06:17 on Nov 14, 2012

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
Yes, the index of the elements does start at 0.

Take a look at this page: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

code:
int[] newArray = {};
With the above line, you are using the "shortcut syntax" mentioned on that page. In this case, you are creating an array of length 0. The shortcut syntax doesn't really gain you anything here, probably better to use the other method of initializing an array.

Insane Totoro
Dec 5, 2005

Take cover!!!
That Totoro has an AR-15!

fletcher posted:

Yes, the index of the elements does start at 0.

Take a look at this page: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

code:
int[] newArray = {};
With the above line, you are using the "shortcut syntax" mentioned on that page. In this case, you are creating an array of length 0. The shortcut syntax doesn't really gain you anything here, probably better to use the other method of initializing an array.

Thanks, that was a big mistake.

Also see above, I seem to have misunderstood the original question!

Turkeybone
Dec 9, 2006

:chef: :eng99:
With the edits and going back and forth I'm confused as to where you are at on this.

edit: public static int[] makeArray(int value)

Are you supposed to type in some long number and return each single digit as part of an array?

edit2: Yeah that is what it seems to do. I don't know how much of an answer you want, but yes, examine your for loop there.

edit3: What are you using to run these things?

Turkeybone fucked around with this message at 07:17 on Nov 14, 2012

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
Try stepping through your makeArray code in the debugger. There's a fairly big error there that will become obvious when you do this :)

zarg
Mar 19, 2005

We are the Unity 3D community. You will be assimilated. Resistance is futile.
Hi guys. Pretty short question here, but I'm having some trouble and could use a hand. I'm using charAt and isLetter for the first time, and I'm having some unexpected issues with these two lines of code.

code:
for (int i = 0; 1 < (str.length() - 1); i++)
	if (str.charAt(i).isLetter());
I'm getting "cannot invoke isLetter on primitive data type char," but I'm pretty sure I should be able to... anyone know what I'm missing here?



Context & extra information, in case it's needed:

I am writing a letter frequency counter. What I'm trying to do is look at each letter of each line of the file I'm reading in, check to see if it's a letter, and if it is I'll increment my counter array at the appropriate location. Here is a list of the imports I've got at the top of my class:

code:
import java.util.Arrays;
import java.io.*;
import java.net.URL;
import java.util.Scanner;
edit:

Ah, I think I got it... for some reason you have to manually prefix "isletter" with "Character." Weird. The below seems to be working for now. (No errors in Eclipse, at least; we'll see about run time! :) )

code:
for (int i = 0; 1 < (str.length() - 1); i++)
	if (Character.isLetter(str.charAt(i)));

zarg fucked around with this message at 04:42 on Nov 16, 2012

Titan Coeus
Jul 30, 2007

check out my horn

zarg posted:

isLetter question

The reason for this is that primitive data types, like characters, don't have methods attached to them. Calling the 'isLetter' method on a primitive character doesn't work in Java, but passing the character as an argument to the isLetter method on the 'Character' class does.

zarg
Mar 19, 2005

We are the Unity 3D community. You will be assimilated. Resistance is futile.

Titan Coeus posted:

The reason for this is that primitive data types, like characters, don't have methods attached to them. Calling the 'isLetter' method on a primitive character doesn't work in Java, but passing the character as an argument to the isLetter method on the 'Character' class does.

Right, OK, that makes sense. Thanks for clarifying :)

Turkeybone
Dec 9, 2006

:chef: :eng99:
Those are called wrappers, they go around primitives so you can use functions like that (see also like Integer.parseInt) and so you can put them into other structures that dont take primitives (vectors and idk array lists?)

Titan Coeus
Jul 30, 2007

check out my horn

Turkeybone posted:

Those are called wrappers, they go around primitives so you can use functions like that (see also like Integer.parseInt) and so you can put them into other structures that dont take primitives (vectors and idk array lists?)

While Character is a wrapper class for the char primitive, in this case it isn't being used as such. Character.isLetter is a static method, so no instance of the Character class is made (and thus nothing is being wrapped). The purpose of static methods on wrapper classes is for organizational purposes.

Insane Totoro
Dec 5, 2005

Take cover!!!
That Totoro has an AR-15!
I am trying to add together the values in the columns of a jagged 2D array.

Like....

2 3 4 5 6
3 6 6
1 2 3 9 8 4

How do I get around a boundary exception error? It is simple enough to write a loop that adds array[1][1] array [2][1] etc etc etc but in the fourth column you have a gap there.... We literally just learned about arrays this week.

Joda
Apr 24, 2010

When I'm off, I just like to really let go and have fun, y'know?

Fun Shoe

Insane Totoro posted:

I am trying to add together the values in the columns of a jagged 2D array.

Like....

2 3 4 5 6
3 6 6
1 2 3 9 8 4

How do I get around a boundary exception error? It is simple enough to write a loop that adds array[1][1] array [2][1] etc etc etc but in the fourth column you have a gap there.... We literally just learned about arrays this week.

Working with non-rectangular 2D arrays seems unnecessarily counter-intuitive for so many reasons. Can't you just define the empty values as 0 in a [i][j] array? If not I'd recommend first sorting the sub-arrays by length, with the longest one at position 0, and then design your loop conditions around that.

EDIT:
Actually, I guess you could work around it by making a conditional based on the length of the current sub-array using continue. (Note: you're going to have to find the longest sub-array first)

Example:
Java code:
int sum;
for (int j = 0; j < lengthOfLongestSubArray; j++) {
	sum = 0;
	for (int i = 0; i < array.length; i++) {
		if (j > array[i].length)
			continue;
		else
			sum = sum + array[i][j];
	}
	//Insert code to return/store/print sum
}
I think the above code should work for your need. Let me know if my solution isn't immediately clear/doesn't work.

E2:
vvvv The code I edited in should work with the array as is. I guess sorting them would allow you to use break rather than continue, which should require less system resources.

Joda fucked around with this message at 20:36 on Nov 17, 2012

Insane Totoro
Dec 5, 2005

Take cover!!!
That Totoro has an AR-15!
For reference this is what I have so far.

I'm actually not sure what you mean sorting the arrays? Like rearranging them first? That actually sounds..... waitaminute, so sort the arrays by length first and then add? Wouldn't you still have gaps just because it's not a perfect 2D rectangle?

Java code:
 public static void arrayColumnAdder(int[][] columns){
      System.out.print("The sum of the columns is... ");
       
      for (int i = 0 ; i < columns.length; i++){
       
        int sum = 0;
        for (int j = 0 ; j < columns[i].length; j++){
        sum += columns[i][j];
        }
        
        System.out.print(sum + " ");
      }
 }

Joda
Apr 24, 2010

When I'm off, I just like to really let go and have fun, y'know?

Fun Shoe
Just to make everything clear, you're supposed to add 2+3+1 and 3+6+2 etc. right? The code you have just seems to add the contents of every sub array and I can't really tell how you can get a index out of bounds exception.

EDIT: I might have mixed up the dimensions in my example at a second look. The general principle remains the same though. Nevermind it should work as is.

E2: If I have the right idea about the assignment, the general concept is two-fold:
1) Consider the arrays the opposite way from what you're used to, in the sense that for every individual sum you're actually iterating through the same position of each sub-array (I assume you've been taught how a 2D array is just an array of arrays. What I refer to as a sub-array must be the dimension which is variable in size.)
2) Every time you add a number from a sub-array, you have to make sure the position is not out of bounds for that particular sub-array. This is achieved by comparing the length of it with the current value of j and if the length is smaller than j, you're out of bounds and you use continue. (continue just returns the program to the start of the loop without executing the rest of the code after continue. As opposed to break which stops the loop entirely.)

Hope that helps.

Joda fucked around with this message at 00:11 on Nov 18, 2012

Insane Totoro
Dec 5, 2005

Take cover!!!
That Totoro has an AR-15!

Joda posted:

Working with non-rectangular 2D arrays seems unnecessarily counter-intuitive for so many reasons. Can't you just define the empty values as 0 in a [i][j] array? If not I'd recommend first sorting the sub-arrays by length, with the longest one at position 0, and then design your loop conditions around that.

EDIT:
Actually, I guess you could work around it by making a conditional based on the length of the current sub-array using continue. (Note: you're going to have to find the longest sub-array first)

Example:
Java code:
int sum;
for (int j = 0; j < lengthOfLongestSubArray; j++) {
 sum = 0;
 for (int i = 0; i < array.length; i++) {
  if (j > array[i].length)
   continue;
  else
   sum = sum + array[i][j];
 }
 //Insert code to return/store/print sum
}
I think the above code should work for your need. Let me know if my solution isn't immediately clear/doesn't work.

E2:
vvvv The code I edited in should work with the array as is. I guess sorting them would allow you to use break rather than continue, which should require less system resources.

Ohhhhh. I see what you did there. It is forcing the loop to stop under certain circumstances and looking at the problem from a different geometric perspective

Joda
Apr 24, 2010

When I'm off, I just like to really let go and have fun, y'know?

Fun Shoe
That sounds about right. Continue and break are both good to know precisely for cases like these. Its use is fairly intuitive in this case, though. The hardest part seems to be how the task "tricks" you by reversing the order in which you iterate through the arrays.

Also: The conditional should be "if (j >= array[i].length)" rather than "if (j > array[i].length)"

Insane Totoro
Dec 5, 2005

Take cover!!!
That Totoro has an AR-15!
Yeah I knew about continue and break but this is the first time I had ever had a use for it. Really appreciate that you helped me remember the application of those commands!

zarg
Mar 19, 2005

We are the Unity 3D community. You will be assimilated. Resistance is futile.
I'm trying to write a program that asks the user to provide either a file or a URL, then does some stuff that doesn't really matter for this question. (The "some stuff" code already works if I hard-code the decision in, so that's no problem)

Basically, I can't get the user's input to trigger my "if, else if" statements. I'm sure I'm overlooking something silly about how input is handled or something, but I'm not sure what I need to do to resolve it.

Here is what I'm working with:

code:
Scanner scanchoice = new Scanner(System.in);
System.out.println ("Would you like to provide a URL or File?");
input = scanchoice.nextLine();
scanchoice.close();
		
		
	if (input == "File")
	{
		//Some stuff
	}
		
	else if (input == "URL")
	{
		//Some stuff
	}
I put some simple "got here!" code into the if/else if statements, and it never gets triggered no matter what I enter.

What the devil am I doing wrong?

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

== only tests for reference equality. You'll want input.equals("File") for string equality.

zarg
Mar 19, 2005

We are the Unity 3D community. You will be assimilated. Resistance is futile.

carry on then posted:

== only tests for reference equality. You'll want input.equals("File") for string equality.

Ah, fantastic, thank you very much.

pigdog
Apr 23, 2004

by Smythe

carry on then posted:

== only tests for reference equality. You'll want input.equals("File") for string equality.

Better yet "File".equals(input), so that in case input is null (which won't happen here but would in most code), the comparison would simply return false rather than throw a NullPointerException.

Max Facetime
Apr 18, 2009

pigdog posted:

Better yet "File".equals(input), so that in case input is null (which won't happen here but would in most code), the comparison would simply return false rather than throw a NullPointerException.

This was the idiom in Java 6 and earlier, but Java 7 introduced switch-case on Strings. However, explicitly checking for null values is again recommended if null is a possible value at that point, otherwise switch throws a NullPointerException.

zarg
Mar 19, 2005

We are the Unity 3D community. You will be assimilated. Resistance is futile.

pigdog posted:

Better yet "File".equals(input), so that in case input is null (which won't happen here but would in most code), the comparison would simply return false rather than throw a NullPointerException.

Used this suggestion and got it working no problem. I now know that War and Peace has the letter Z in it 2279 times!

(It was a simple letter frequency program, with the added bonus of reading in a file or URL at run-time)

Thanks very much guys :)

Kilson
Jan 16, 2003

I EAT LITTLE CHILDREN FOR BREAKFAST !!11!!1!!!!111!
code:
DateFormat df = new SimpleDateFormat();
df.setTimeZone(TimeZone.getTimeZone("Etc/GMT-5"));
System.out.println(df.format(new Date()));
With the above, I get '11/20/12 2:14 AM'
When I use "GMT+5", '11/19/12 4:14 PM'
Actual GMT time is 11/19/12 9:14 PM

Why does this seem to do the GMT offset backwards?

So apparently this is some sort of weird POSIX thing:

tzdb posted:

# We use POSIX-style signs in the Zone names and the output abbreviations,
# even though this is the opposite of what many people expect.
# POSIX has positive signs west of Greenwich, but many people expect
# positive signs east of Greenwich. For example, TZ='Etc/GMT+4' uses
# the abbreviation "GMT+4" and corresponds to 4 hours behind UTC
# (i.e. west of Greenwich) even though many people would expect it to
# mean 4 hours ahead of UTC (i.e. east of Greenwich).

Does anyone have any recommendations on how to let an end-user specify what timezone they are in, without presenting them the entire list of ~600 choices? The JVM seems to support some simple abbreviations like CST/EST/etc., but I don't think those handle DST. JodaTime doesn't support those abbreviations at all.

Kilson fucked around with this message at 23:22 on Nov 19, 2012

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
I can't remember what time zone I'm in, and neither can anyone else. You can be pretty sure they know what city and/or country they are in, though.

Personally, my preferred input method is a big list of time zones, labelled by what countries (when they fit into one time zone) and large cities are in them. Sprinkle in some geoip stuff to get a good first guess, and the big list is perfectly usable.

Thom Yorke raps
Nov 2, 2004


Kilson posted:

code:
DateFormat df = new SimpleDateFormat();
df.setTimeZone(TimeZone.getTimeZone("Etc/GMT-5"));
System.out.println(df.format(new Date()));
With the above, I get '11/20/12 2:14 AM'
When I use "GMT+5", '11/19/12 4:14 PM'
Actual GMT time is 11/19/12 9:14 PM

Why does this seem to do the GMT offset backwards?

So apparently this is some sort of weird POSIX thing:


Does anyone have any recommendations on how to let an end-user specify what timezone they are in, without presenting them the entire list of ~600 choices? The JVM seems to support some simple abbreviations like CST/EST/etc., but I don't think those handle DST. JodaTime doesn't support those abbreviations at all.
Java dates are pretty terrible, I would use JodaTime instead for pretty much anything involving dates and Java

Adbot
ADBOT LOVES YOU

Kilson
Jan 16, 2003

I EAT LITTLE CHILDREN FOR BREAKFAST !!11!!1!!!!111!

Ranma posted:

Java dates are pretty terrible, I would use JodaTime instead for pretty much anything involving dates and Java

I had already tried that, and Joda does exactly the same thing with backward GMT offsets. :eng99:

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply