I am posting DateTime as JSON and it becomes "/Date(1512839439513)/"
i simply want to convert
"/Date(1512839439513)/" to java.util.Date
I have tried this
String date = finalObject.getString("DateCreated");
String datereip = date.replaceAll("\\D+","");
Long timeInMillis = Long.parseLong(datereip);
Date date1=new Date(timeInMillis);
But did not worked...
The way you extract the milliseconds from the string seems to be the problem.
You can try this to extract needed data from the string:
String date = finalObject.getString("DateCreated");
String temp = date.substring(date.indexOf("(") + 1);
String datereip = date.substring(0, date.indexOf(")"));
Long timeInMillis = Long.parseLong(datereip);
Date date1=new Date(timeInMillis);
This assumes that the date string will have only one pair of parenthesis. Also, there are better ways to extract string between 2 chars with Java but that is a topic of another question.
Related
Hi I have a String like this "01-09-2015"
I need to convert this string to long.
I have tried using
String date = "01-09-2015";
Long dateLong = Long.getLong(date);
Long dateLong = Long.valueOf(date);
Long dateLong = Long.valueOf(date,36);
Long dateLong = Long.parseLong(date);
Still no help. Everything returns be NumberFormatException.
Anyone please help.
Thanks in advance.
EDIT
Guys, I will send in a String, which is of course a date like "08-01-2015". For each unique string which I pass, I need a unique long value. Thats all.
You have to convert it to Date first before changing it to Long
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yy");
Date d = format.parse("01-09-2015");
long milliseconds = d.getTime();
I recommend you split the input on -, then convert the three date parts to int; and then concatenate them into a long. Something like,
String date = "01-09-2015";
String[] arr = date.split("-");
int yyyy = Integer.valueOf(arr[2]);
int mm = Integer.valueOf(arr[0]);
int dd = Integer.valueOf(arr[1]);
long dateLong = (yyyy * 10000) + (mm * 100) + dd;
System.out.println(dateLong);
Output is (the unique, per date)
20150109
Note This is in keeping with ISO 8601; the linked Wikipedia article says (in part)
ISO 8601 Data elements and interchange formats – Information interchange – Representation of dates and times is an international standard covering the exchange of date and time-related data.
I have a string "1427241600000" and I want it converted to "yyyy-MM-dd" format.
I have tried, but I am not able to parse it, please review the below code
try {
String str = "1427241600000";
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
Date date =sf.parse(str);
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}
I would like to know where I went wrong.
You should try it the other way around. First get the Date out of the milliTime and then format it.
String str = "1427241600000";
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date(Long.parseLong(str));
System.out.println(sf.format(date));
the conversion is highly dependent on what format the timestamp is in. But i assume the whole thing should actually be a long and is simply the systemtime from when the timestamp was created. So this should work:
String str = ...;
Date date = new Date(Long.parseLong(str));
Use Date date =new Date(Long.parseLong(str)); to convert your String to Date object.
if you are using SimpleDateFormat() the format specified as a parameter to this function should match the format of the date in the String (str in your case). In your case yyyy-MM-dd does not match the format of the time stamp (1427241600000).
You can do it like this:
use a SimpleDateFormat with an appropriate format string (be careful to use the correct format letters, uppercase and lowercase have different meanings!).
DateFormat format = new SimpleDateFormat("MMddyyHHmmss");
Date date = format.parse("022310141505");
Here is the code I am using:
String string = "08/07/2013".replace('/', '-');
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(string);
Why does date return: "Wen Jan 3 00:00:00 EST 14"? It is not at all the date format I told it to use.
Edit: I need this format because a database I am using requires this format.
The format you use to parse the date string, does not match it. You are using yyyy for 08.
Use the following format:
new SimpleDateFormat("dd-MM-yyyy")
and why at all are you replacing the / with -? You can build the pattern for your original string only:
String string = "08/07/2013"
Date date = new SimpleDateFormat("dd/MM/yyyy").parse(string);
and if you want your date string in yyyy-MM-dd format, then you can format the date using DateFormat#format(Date) method:
String formattedDate = new SimpleDateFormat("yyyy-MM-dd").format(date);
See also:
Change date format in a Java string
SimpleDateFormat Java Doc
When you are specifying some Simple Date format using string e.g. "yyyy-MM-dd" , you have to provide your date in same format to get a date object eg. "1991-07-24" .
String mydate = "1991/07/24";
Date formattedDate = new SimpleDateFormat("yyyy/MM/dd").parse(mydate);
now if you want to convert it in any other format, you can do that by FORMATTING this date object into that perticular format..
String dateInOtherFormat = new SimpleDateFormat("dd-MMM-yyyy").format(formatteddate);
and the output of dateInOtherFormat will be ... 24-JUL-1991 .
Ok my sugestion is stupid but if you need this format try this
String[] arr = "08/07/2013".split("/");
String newString = arr[2]+"-"+arr[1]+"-"arr[0];
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(newString);
NOTE If your original strin is in format "MM/dd/YYYY use this:
String newString = arr[2]+"-"+arr[0]+"-"arr[1];
I looked through all possible answer here but I am having hard time to figure this thing out.
I have Json date in a String. I want to convert into a Java Date without losing time.
Also I would like to convert from Java Date to Json Date string.
Here what I have.
String jsonDateString = "/Date(1295157600000-0600)/";
There are 2 parts in your time : the local time in milliseconds, and the offset in hours and minutes. You have to parse them and "add" them to get the milliseconds UTC.
You may do it using this function :
private static Pattern p = Pattern.compile("\\((\\d+)([+-]\\d{2})(\\d{2})\\)");
public static Date jd2d(String jsonDateString) {
Matcher m = p.matcher(jsonDateString);
if (m.find()) {
long millis = Long.parseLong(m.group(1));
long offsetHours = Long.parseLong(m.group(2));
long offsetMinutes = Long.parseLong(m.group(3));
if (offsetHours<0) offsetMinutes *= -1;
return new Date(
millis
+ offsetHours*60l*60l*1000l
+ offsetMinutes*60l*1000l
);
}
return null;
}
To make "back" a JSON date, I would simply encode the UTC time :
String jsonDate = "/Date("+date.getTime()+"+0000)/";
Hi I have the following date string format:
June-2008
July-2008
March-2010
May I enquire what is the java regex to extract out whatever the month and year value into separate string each. The delimeter here is the "-" sign
Any values from the start to the end of the "-" sign is the month, anything after the "-" sign is the year value.
The following is what I would like to achieve
String m = March
String y = 2010
Thank all for your help!
Don't use a regex. Use String#split():
String[] parts = "June-2008".split("-", 2); // or .split("-")
String m = parts[0]; // "June"
String y = parts[1]; // "2008"
Even better, use SimpleDateFormat, and you'll have a proper Date to work with:
DateFormat df = new SimpleDateFormat("M-y");
Date myDate = df.parse("June-2008");
I want to extend the answer given by Matt Ball. If you want to split that string and willing to use other than String#split() you can use StringUtils of Apache Commons Lang. It has various String utility methods.
An example:
String strDate = "June-2008";
String[] parts = StringUtils.split(strDate, '-');
String month = parts[0]; // "June"
String year = parts[1]; // 2008
Also if you want to get a java.util.Date object then Joda Time may helps you. I personally prefer this api rather java.util.Date as it is far more rich, easy-to-use, less error-pron.
Now we can manipulate that String as:
String strDate = "June-2008";
DateTimeFormatter formatter = DateTimeFormat.forPattern("MMMM-yyyy");
DateTime dateTime = formatter.parseDateTime(strDate); // parse it
Date javaDate = dateTime.toDate(); // a java.util.Date object
int year = dateTime.getYear(); // 2008
String month = dateTime.month().getAsText(); // June
Hope this will help you. Thanks.