The date format issue from java - java

I get the date format from a joynet cloud api server:
2012-11-20T10:26:04+00:00"
However, I have no idea to handle the last segment +00:00, I have made the format except for +00:00
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date date = fmt.parse("2012-11-20T10:26:04");
Thanks for #Abu
I rewrite it to remove ":",
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
String input = "2012-11-20T10:25:58+00:00";
String s1 = input.split("T")[0];
String s2 = input.split("T")[1];
String sep = null;
if (s2.contains("+")) {
sep = "+";
}
if (s2.contains("-")) {
sep = "-";
}
String s3 = s2.split("\\" + sep)[0];
String s4 = s2.split("\\" + sep)[1].replace(":", "");
String cleanDate = s1 + "T" + s3 + sep + s4;
Date date = fmt.parse(cleanDate);
System.out.println(date);

Remove that : inside the time zone part if you are not using java 7
and use this :
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
See javadoc for SimpleDateFormat in Java 6
And
If you are using Java 7 then directly use this :
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
See javadoc for SimpleDateFormat in Java 7

Related

How do I convert an Arabic String date to a java 8 date object?

I'm working on a web monitoring project in Arabic and I want to convert string date like this one:
الاثنين 24 أبريل 2017 - 15:00
to Java 8 date object. How can I do that?
Edit: with thanks to slim and Meno Hochschild for inspiration:
String dateTimeString = "الاثنين 24 أبريل 2017 - 15:00";
DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("EEEE d MMMM uuuu - HH:mm", new Locale("ar"));
LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, formatter);
System.out.println(dateTime);
This prints:
2017-04-24T15:00
The answers of #Ole and #slim are working, but not for the reason they think.
First observation - the nu-extension is unnecessary for given example:
Oles suggestion would also work for the locale new Locale("ar", "SA") instead of Locale.forLanguageTag("ar-SA-u-nu-arab"). So what does the unicode-nu-extension here? Nothing. Next question:
What is the nu-extension supposed to do here?
The nu-code-word "arab" is specified by the unicode consortium to yield arabic-indic digits. But the input to be parsed does only have western digits 0-9 (which are historically overtaken from Arab people and specified as code word "latn" - a misnomer by the way). So if the nu-extension had really done its job here then parsing should have failed because arabic-indic digits are not 0-9 but:
٠ ١ ٢ ٣ ٤ ٥ ٦ ٧ ٨ ٩
Obviously, the nu-extension is not supported in general by new time-API in Java-8.
Does SimpleDateFormat support the nu-extension?
Using debugging of following code, I discovered that the nu-extension is only supported for Thai-numerals (see also official javadoc of class java.util.Locale but not for arabic-indic digits:
SimpleDateFormat sdf =
new SimpleDateFormat("EEEE d MMMM yyyy - HH:mm", Locale.forLanguageTag("ar-SA-nu-arab"));
Date d = sdf.parse(dateTimeString);
System.out.println(d);
String formatted = sdf.format(d);
System.out.println(formatted);
System.out.println(sdf.format(d).equals(dateTimeString));
sdf = new SimpleDateFormat("EEEE d MMMM uuuu - HH:mm", Locale.forLanguageTag("ar-SA-u-nu-thai"));
String thai = sdf.format(d);
System.out.println("u-nu-thai: " + thai);
I assume the class DateTimeFormatter of Java-8 also supports Thai numerals.
Conclusion:
Forget the nu-extension. Just construct the locale via the old-fashioned way without unicode extension and adapt Oles answer this way. It works because your input only has western digits 0-9.
For extensive i18n-support including the nu-extension for various numbering systems (if you have such input), you might consider external libraries (for example ICU4J or my lib Time4J).
I don't know enough Arabic to understand an Arabic formatted date. However this code:
Locale arabicLocale = new Locale.Builder().setLanguageTag("ar-SA-u-nu-arab").build();
LocalDate date = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).withLocale(arabicLocale);
String formatted = date.format(formatter);
System.out.println(formatted);
System.out.println(formatter.parse(formatted));
Yields this output:
26 أبريل, 2017
{},ISO resolved to 2017-04-26
The code to create the Locale is from an answer to Setting Arabic numbering system locale doesn't show Arabic numbers
You can fine-tune this format by defining your own FormatStyle.
You have to specify the charset when parsing the string, assuming that the date you want to parse will always be in the format you provided this would work :
public static Date getDate(String strDate) throws Exception{
strDate=new String(strDate.getBytes(),"UTF-8");
Map<String, Integer> months = new HashMap<>();
String JAN = new String("يناير".getBytes(), "UTF-8");
String FEB = new String("فبراير".getBytes(), "UTF-8");
String MAR = new String("مارس".getBytes(), "UTF-8");
String APR = new String("أبريل".getBytes(), "UTF-8");
String APR_bis = new String("ابريل".getBytes(), "UTF-8");
String MAY = new String("ماي".getBytes(), "UTF-8");
String JUN = new String("بونيو".getBytes(), "UTF-8");
String JUN_bis = new String("يونيه".getBytes(), "UTF-8");
String JUL = new String("يوليوز".getBytes(), "UTF-8");
String AUG = new String("غشت".getBytes(), "UTF-8");
String SEP = new String("شتنبر".getBytes(), "UTF-8");
String SEP_bis = new String("سبتمبر".getBytes(), "UTF-8");
String OCT = new String("أكتوبر".getBytes(), "UTF-8");
String OCT_bis = new String("اكتوبر".getBytes(), "UTF-8");
String NOV = new String("نونبر".getBytes(), "UTF-8");
String NOV_bis = new String("نوفمبر".getBytes(), "UTF-8");
String DEC = new String("دجنبر".getBytes(), "UTF-8");
String DEC_bis = new String("ديسمبر".getBytes(), "UTF-8");
months.put(JAN, 0);
months.put(FEB, 1);
months.put(MAR, 2);
months.put(APR, 3);
months.put(APR_bis, 3);
months.put(MAY, 4);
months.put(JUN, 5);
months.put(JUN_bis, 5);
months.put(JUL, 6);
months.put(AUG, 7);
months.put(SEP, 8);
months.put(SEP_bis, 8);
months.put(OCT, 9);
months.put(OCT_bis, 9);
months.put(NOV, 10);
months.put(NOV_bis, 10);
months.put(DEC, 11);
months.put(DEC_bis, 11);
StringTokenizer stringTokenizer = new StringTokenizer(strDate);
Calendar calendar = Calendar.getInstance();
while(stringTokenizer.hasMoreElements()) {
stringTokenizer.nextElement();// to skip the first string which is the name of the day
int day = Integer.parseInt(stringTokenizer.nextElement().toString().trim());
String strMonth = stringTokenizer.nextElement().toString().trim();
int month = months.get(strMonth);
int year = Integer.parseInt(stringTokenizer.nextElement().toString().trim());
calendar.set(year, month, day);
}
return calendar.getTime();
}
it gives this output :
Fri Oct 20 15:26:47 WEST 2017
One solution could be to translate the date to English and parse it then:
private final static Map<String, Integer> monthMapping = new HashMap<>();
static {
// list of all month.
monthMapping.put("أبريل", "4");
}
public Date fromArabicToDate(String arabicInput) throws ParseException {
String[] parts = arabicInput.split(" ");
if (parts.length != 4)
throw new IllegalArgumentException();
String dateInput = parts[0] + "-" + monthMapping.get(parts[1]) + "-" + parts[2];
SimpleDateFormat parser = new SimpleDateFormat("YYYY-MM-DD");
return parser.parse(dateInput);
}
I tried to copy the month but I don't believe I have done it correctly. The arguments of put get switched when parsing.
Or you have a look at Joda-Time. Maybe they have a solution. It was mentioned here.

LDAP TIME TO REQUIRED FORMAT

Currently I get 20150211152026.0Z this format from ldap now I would like to store this in my database in this format YYYY-MON-DD HH:mm:ss with java.
Please guide how this could be achieved.
As specified in this question, you can do sthg like this;
String[] parts = inputDateTime.split("[.]");
String dateTimePart = parts[0];
String timeZonePart = "+0" + parts[1].substring(0, parts[1].length() - 1) + "00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssZ");
Date theDate = sdf.parse(dateTimePart + timeZonePart);
Thanks to highlycaffeinated
Updated version for your comment;
String inputDateTime = "20150211152026.0Z";
String[] parts = inputDateTime.split("[.]");
String dateTimePart = parts[0];
String timeZonePart = "+0" + parts[1].substring(0, parts[1].length() - 1) + "00";
DateFormat originalFormat = new SimpleDateFormat("yyyyMMddHHmmssZ", Locale.ENGLISH);
DateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = originalFormat.parse(dateTimePart+timeZonePart);
String formattedDate = targetFormat.format(date);
System.out.println(formattedDate);

Error converting Date from one format to another format

Having trouble in the following code. The output is dateStr: 11-Jan-11. Can anyone tell me why the date is modified?
String dateStr="";
String actionCompletionDueDate = "16/11/2011";
DateFormat srcDf = new SimpleDateFormat("mm/dd/yyyy");
DateFormat destDf = new SimpleDateFormat("dd-MMM-yy");
if(actionCompletionDueDate != null && !actionCompletionDueDate.equals("")) {
// parse the date string into Date object
System.out.println("actionCompletionDueDate: " + actionCompletionDueDate);
Date actionCompletionDate = srcDf.parse(actionCompletionDueDate);
dateStr = destDf.format(actionCompletionDate);
System.out.println("dateStr: " + dateStr);
}
Change
DateFormat srcDf = new SimpleDateFormat("mm/dd/yyyy");
to
DateFormat srcDf = new SimpleDateFormat("dd/MM/yyyy");
OR pass a correct string (which respects your format) to your code
String actionCompletionDueDate = "11/16/2011";
and correct the format to DateFormat srcDf = new SimpleDateFormat("MM/dd/yyyy");
mm is for minutes and MM is for months
String actionCompletionDueDate = "16/11/2011";
Should be
String actionCompletionDueDate = "11/16/2011";
Change
DateFormat srcDf = new SimpleDateFormat("mm/dd/yyyy");
to
DateFormat srcDf = new SimpleDateFormat("MM/dd/yyyy");
small mm here corresponds to minute.
But if you print source date using,
System.out.println(actionCompletionDate.toString());
Output is :
Sun Jan 16 00:11:00 IST 2011
See, 11 minute in time.
And change source date too, to 11/16/2011.

How to format my date with SimpleDateFormat?

I'm trying to set a date format, but when i run this code
String oldstring = "2013-01-1";
System.out.println("oldstring = "+oldstring);
Date date = new SimpleDateFormat("yyyy-mm-dd").parse(oldstring);
System.out.println("datefield = "+date);
i take result:
oldstring = 2013-01-1
datefield = Tue Jan 01 00:01:00 MSK 2013
Why datefield isn't equal 2013-01-1?
At first mm in yyyy-mm-dd mean minute not Month. to set month use MM.
It would be look like this :
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(oldstring);
UPDATE
Try this:
String oldstring = "2013-01-1";
System.out.println("oldstring = "+oldstring);
Date date = new SimpleDateFormat("yyyy-mm-dd").parse(oldstring);
String sdf = new SimpleDateFormat("yyyy-mm-dd").format(date);
System.out.println("datefield = "+sdf);
If you don't use new SimpleDateFormat("yyyy-mm-dd").format(date);
you getting standard date format which include all info. If you want special format you need to use
new SimpleDateFormat("yyyy-mm-dd").format(date);
Also read this article about date formatting
The type of datefield is Date, so the toString method will basically always return the same format, as you are not overriding it.
So what you need to do, is basically:
String oldstring = "2013-01-1";
System.out.println("oldstring = "+oldstring);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(oldstring);
System.out.println("datefield = "+date);
String outDateStr = sdf.format(date);
System.out.println("newstring = "+outDateStr);
Use MM for month. mm is for minutes

How to convert date format in java?

I have a String a ="   December 2011   ";
There is a before and after it.
How can I Convert it to: String b = "2011-12-1"
If this is really all there is to it, use .replace():
String b = a.replace(" ", "");
Unlike what its name would let you believe since .replaceAll() exists, .replace() will actually replace all occurrences. The difference is that .replaceAll() expects a string which will be compiled as a Pattern as an argument.
Use SimpleDateFormat.
new SimpleDateFormat("yyyy-MM-d").format(new SimpleDateFormat("MMMMM yyyy").parse(str));
String a = " Dezember 2011 ";
SimpleDateFormat sdf = new SimpleDateFormat("MMMMM yyyy");
a = a.replace(" ", "");
Date parse = sdf.parse(a);
SimpleDateFormat outSdf = new SimpleDateFormat("yyyy-MM-d");
String out = outSdf.format(parse);
System.out.println(out);
You use this code.It gives whatever you want to output.
You first remove &nbsp using replace function and after you have to just parse the your string.
try {
String a = " December 2011 ";
a = a.replace(" ", "");
SimpleDateFormat sdf = new SimpleDateFormat("MMMMM yyyy");
SimpleDateFormat SdfParser = new SimpleDateFormat("yyyy-MM-dd");
String date =SdfParser.format(sdf.parse(a));
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}

Categories

Resources