I just created sample BB app, which can allow to choose the date.
DateField curDateFld = new DateField("Choose Date: ",
System.currentTimeMillis(), DateField.DATE | DateField.FIELD_LEFT);
After choosing the date, I need to convert that long value to String, so that I can easily store the date value somewhere in database.
I am new to Java and Blackberry development.
long date = curDateFld.getDate();
How should I convert this long value to String? Also I want to convert back to long from String. I think for that I can use long l = Long.parseLong("myStr");?
See the reference documentation for the String class: String s = String.valueOf(date);
If your Long might be null and you don't want to get a 4-letter "null" string, you might use Objects.toString, like: String s = Objects.toString(date, null);
EDIT:
You reverse it using Long l = Long.valueOf(s); but in this direction you need to catch NumberFormatException
String strLong = Long.toString(longNumber);
Simple and works fine :-)
Long.toString()
The following should work:
long myLong = 1234567890123L;
String myString = Long.toString(myLong);
very simple,
just concatenate the long to a string.
long date = curDateFld.getDate();
String str = ""+date;
1.
long date = curDateFld.getDate();
//convert long to string
String str = String.valueOf(date);
//convert string to long
date = Long.valueOf(str);
2.
//convert long to string just concat long with empty string
String str = ""+date;
//convert string to long
date = Long.valueOf(str);
String logStringVal= date+"";
Can convert the long into string object, cool shortcut for converting into string...but use of String.valueOf(date); is advisable
Just do this:
String strLong = Long.toString(longNumber);
String longString = new String(""+long);
or
String longString = new Long(datelong).toString();
Related
I have a string "$1,076.00" and I want to convert them in to int,
I capture some value $1,076.00 and saved in string called originalAmount,
and tried int edited = Integer.parseInt(originalAmount); and it gave me error java.lang.NumberFormatException: For input string: "$1,076.00"
can anyone help?
You need to remove the undesired part ($ sign) and then parse the string to double carefully since the decimal part is a locale dependent
String pay = "$1,076.00";
NumberFormat format = NumberFormat.getInstance(Locale.FRANCE);
Number number = format.parse(pay.replace("$", ""));
double result = number.doubleValue();
System.out.println(result);
string sourceString = "$1,076.00";
sourceString.substring(1, sourceString.length() - 1)
int foo = Integer.parseInt(sourceString);
Try this:
String amount = "$1,076,.00";
String formatted = amount.replace("$", ""); // remove "$" sign
formatted = formatted.replace(",", ""); // remove "," signs from number
double amountDouble = Double.parseDouble(formatted); // convert to double
int amountInt = (int)amountDouble; // convert double value to int
System.out.println(amountInt); // prints out 1076
String originalAmount="$1076.00";
String amount = originalAmount.replace($,"");
int edited = Integer.parseInt(amount);
Thanks everyone yr answers help me a lot
I have come up with
originalAmount = originalAmount.substring(1);
if (originalAmount.contains("$")) {
originalAmount = originalAmount.replace("$", "");
}
newOriginalAmt = Double.parseDouble(originalAmount);
System.out.println(newOriginalAmt);
pls let me know yr thoughts
I need to compose an URL string in javaME (which doesn't allow me to work with many libraries which would do this in a no time) like this
url = "http://helloworld.com/index.php?data={\"Word\":"+wordX+",\"RS\":20/04/13-2008:31:44,\"SER\":"+net2+"}";
This is producion a string like this:
http://helloworld.com/index.php?data={"Word":358741051173885,"RS":20/04/13-2008:31:44,"SER":53543d303b44523d4e616fd}
The thing is I need the values to also be inside commas. I have tried everything but I'm not being able to understand how to do it.
Can someone explain this?
Thanks
Like this?
String url = String.format("http://helloworld.com/index.php?data={\"Word\":\"%s\",\"RS\":20/04/13-2008:31:44,\"SER\":\"%s\"}", wordX, net2);
Create a MessageFormat:
final MessageFormat urlFormat = new MessageFormat("http://helloworld.com/index.php?data={\"Word\":\"{0}\",\"RS\":20/04/13-2008:31:44,\"SER\":\"{1}\"}");
Then simply apply your values to the format:
final String url = urlFormat.format(new Object[]{wordX, net2});
Or, create a format String:
final String urlFormat = "http://helloworld.com/index.php?data={\"Word\":\"%s\",\"RS\":20/04/13-2008:31:44,\"SER\":\"%s\"}";
And use String.format:
final String url = String.format(urlFormat, wordX, net2);
Or, just change you String to:
url = "http://helloworld.com/index.php?data={\"Word\":\""+wordX+"\",\"RS\":20/04/13-2008:31:44,\"SER\":\""+net2+"\"}";
String val1 = "";
String val2 = "";
String str = "http://helloworld.com/index.php?data={\"Word\":\""+val1+"\",\"RS\":\"20/04/13-2008:31:44\",\"SER\":\""+val2+"\"}";
Im having an issue trying to convert $16500.00 String to 16500 Integer.
This is what i have at the moment but its failing with:
FATAL EXCEPTION: main
java.lang.NumberFormatException: Invalid int: "[]"
The code i have is:
String depTest = mDepositAmount.getText().toString();
String deptest2 = Arrays.toString(depTest.replace("R", "").replace(",", "").split("."));
int dep = Integer.parseInt(deptest2);
Please could you help me with getting the end result to 16500. I know how to convert it to int by using Integer.parseInt its just im struggling to get the end result to be 16500 in String
Does your original string always starts with character '$' and followed by number format?
If so try this one:
String org = "$16500.00";
String numbersOnly = org.substring(1); // "16500.00"
int yourInteger = (int)(Float.parseFloat(numbersOnly));
// if you need String, convert it to String again
String integerString = Integer.toString(yourInteger);
You could use a DecimalFormat
import java.text.*;
NumberFormat nf = new DecimalFormat("$0.00");
int value = (int) nf.parse("$16500.00");
You can try with this
String getValue = "$16500.00";
String removeFirstCharecter = getValue.substring(1); // "16500.00"
String [] getString = removeFirstCharecter.split("\\.");
String firstIntValue = (getString[0]); //16500
String sirstIntValue = (getString[1]); //00
Now you can convert firstIntValue String to Integer .
String getRequiredValue = Integer.toString(firstIntValue); //16500
It may be happen because of $ sign so, Just take one another Text view only for Price 16500 and other for $ sign. then convert the Integer.parseInt(textview.getText().toString);
I have the following problem using the substring() Java function.
I have to do the following operation:
I have a String representing a date having the following form: 2014-12-27 (YEARS-MONTH-DAY).
And I want convert it into a String like this: 20141227 (without the space betwen date component).
So I have implemented the following method that use the substring() method to achieve this task:
private String convertDate(String dataPar) {
String convertedDate = dataPar.substring(0,3) + dataPar.substring(5,6) + dataPar.substring(8,9);
return convertedDate;
}
But it don't work well and return to me wrong conversion. Why? What am I missing?
Use replace method which will replace all ocurrences of '-' for '':
private String convertDate(String dataPar) {
return dataPar.replace('-', '');
}
A simple way would be to replace all the occurrences of -. If the separator could be different then maybe using SimpleDateFormat would be better.
private String convertDate(String dataPar) {
return datapar.replaceAll("-", "");
}
Try replaceAll (This ll replace - with "" means it ll remove -) :
private String convertDate(String dataPar) {
if(dataPar.length() > 0){
return dataPar.replaceAll("-","");
}
return "NOVAL";
}
If the input is only a date, you can use the SimpleDateFormat and use a format like yyMMdd http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
I want you to just change the end indeces of the substring() methods as given below
String convertedDate = dataPar.substring(0,4) + dataPar.substring(5,7) + dataPar.substring(8,10);
I tested, It works Fine as you requested :)
private String convertDate(String dataPar) {
final String year = dataPar.substring(0, 4);
final String month = dataPar.substring(5, 7);
final String day = dataPar.substring(8, 10);
return year + month + day;
}
How can I change 2.5E7 to 2,500,000.00?
Try:
DecimalFormat format = new DecimalFormat("#0,0000.00");
String formattedNumber = format.format(number);
See here
If you are trying to parse 2.5E7 then you can do this
long number = Double.valueOf("2.5E7").longValue();