How to get data in java with localization? - java

I have table with data of date.
This is how I calcaulted the date
DateFormat dateFormat = getFormat();
date = dateFormat.parse((String) value).getTime();
if(date != null) {
cell.setValue(dateFormat.format(date));
tableViewer.update(element, null);
}
public static DateFormat getFormat() {
String systemLocale = System.getProperty("user.language"); //$NON-NLS-1$
Locale locale = new Locale(systemLocale);
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
return dateFormat;
}
The date is exist in the screen in format Month(name of the month) date, year
for example Apr 26,2014.
Now I want to get the value of the cell and to get to format of 'yyyy-mm-dd'
The value of the date is Apr 26,2014.
How I can get the result of 2014-04-26 ? also I think that the value in the UI could change according to the localization of the user
I tried
DateFormat dateFormat = getFormat();
Date parse = dateFormat.parse((String) key);
but then all the get method are deprecated and also I didn't get right result for getYear
I am not expert in the date maybe I miss something

Try this:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class CustomFormattedDate {
public static void main(String[] args) {
Date date = new Date();
// if you use DD for Daypattern, you'll get the day of the year, e.g. 119 for the
// 29th April 2014, if you want 29, the day of the month, use dd
SimpleDateFormat df = new SimpleDateFormat("YYYY-dd-MMM", new Locale(System.getProperty("user.language")));
System.out.println(df.format(date));
System.out.println(getDateFormat().format(date));
}
}
Output
2014-29-Apr

Related

Unparseable date error in Java

I want to convert the system date to yyyy-MM-dd format. There are similar questions in SO. I found that I need to parse the date in input format and then convert to the output format. But I am stuck at the first stage itself. I am not able to parse the system date as such (Sat Apr 25 14:44:15 IST 2015).
Here is my MWE:
import java.util.*;
import java.text.*;
public class Test
{
public static void main(String[] args)
{
try
{
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MM dd HH:mm:ss aaa YYYY");
date = dateFormat.parse(date.toString());
System.out.println(date);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
I get the exception as :
Unparseable date: "Sat Apr 25 14:53:33 IST 2015"
Date object can be converted to string of any date format.
String can be converted to date but it will come only in standard date format's but cant be in the one as you want..
If you want to format system date to yyyy-MM-dd format then use:
Date date = new Date();
SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");
String date1 = dateFormater.format(date);
As you specified in comment you want to subtract sql date with current date then just convert the sql date to normal date format.
Like this:
String date = your date;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = dateFormat.parse(date);
Date currentdate = new Date();
Then use calender objects:
Calendar calendar = Calendar.getInstance();
calendar.setTime(date1);
Calendar calendar2 = calendar.getInstance();
calendar2.setTime(currentdate);
long difference = (calendar2.getTimeInMillis() - calendar
.getTimeInMillis()) / 60000;
This will give you the difference between two dates in minutes.
This will work for you
public class Test1 {
public static void main(String[] args) {
try
{
Date date = new Date();
System.out.println(date);
String dateFormat = new SimpleDateFormat("EEE MM dd HH:mm:ss aaa YYYY").format(date);
System.out.println(dateFormat);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
Output
Sat Apr 25 15:10:38 IST 2015
Sat 04 25 15:10:38 PM 2015
I think you should do it like that.
Date date = new Date();
String formattedDate = new SimpleDateFormat("yyyy-MM-dd").format(date);
DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
date = format.parse(formattedDate);
System.out.println(date);
But you should understand difference between "date" and "date format".

java - hibernate - convert date from one format to another

I am trying to convert date from one format to another, the date entered is in this format : 'mm-dd-yyyy' to 'yyyy-mm-dd'.
I received the date from webpage in 'mm-dd-yyyy' format and when I insert this date in mysql using hibernate, the date changes to some anonymous value.
Please help !!!
{
import java.io.*;
import java.text.*;
import java.util.*;
class test{
public static void main(String...s)throws Exception{
Date date ;
String datestr;
DateFormat dateFormat1 = new SimpleDateFormat("mm-dd-yyyy");
DateFormat dateFormat2 = new SimpleDateFormat("yyyy-mm-dd");
date = dateFormat1.parse("01-01-2015");
datestr = dateFormat1.format(date);
System.out.println(date);
System.out.println(datestr);
date = dateFormat2.parse(datestr);
datestr = dateFormat2.format(date);
System.out.println(date);
System.out.println(datestr);
}
}
You can try like this;
DateFormat originalFormat = new SimpleDateFormat("MM-dd-yyyy", Locale.ENGLISH);
DateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = originalFormat.parse("01-21-2013");
String formattedDate = targetFormat.format(date);
System.out.println(formattedDate);
For Date type result;
DateFormat originalFormat = new SimpleDateFormat("mm-dd-yyyy", Locale.ENGLISH);
DateFormat targetFormat = new SimpleDateFormat("yyyy-mm-dd");
Date date = originalFormat.parse("01-21-2013");
String formattedDate = targetFormat.format(date);
java.util.Date dtt = targetFormat.parse(formattedDate);
java.sql.Date ds = new java.sql.Date(dtt.getTime());
System.out.println(ds);
System.out.println(dtt);
System.out.println(formattedDate);
End the output is;
2013-01-21
Mon Jan 21 00:01:00 EET 2013
2013-01-21
you can use this below code snippet
public static String formatDate (String date, String initDateFormat, String endDateFormat) throws ParseException {
Date initDate = new SimpleDateFormat(initDateFormat).parse(date);
SimpleDateFormat formatter = new SimpleDateFormat(endDateFormat);
String parsedDate = formatter.format(initDate);
return parsedDate;
}
Date initDate = new SimpleDateFormat("yyyy-MM-dd").parse("2015-03-05");
System.out.println("initDate == "+initDate);
output ==>> initDate == Thu Mar 05 00:00:00 ICT 2015
In HQL simple SQL function work :
DATE_FORMAT(DATE,'%d-%m-%Y')

issue with date and time zone how to get date without time zone?

I tried to do convert between date in Tics to UTC date time format - 'YYYY-MM-DDThh:mm:ss'
public static DateFormat getFormat() {
String systemLocale = System.getProperty("user.language"); //$NON-NLS-1$
Locale locale = new Locale(systemLocale);
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, locale);
return dateFormat;
}
public static Object getFormatedValue(Object value) {
String updated = (strValue).replaceAll(pattern, "$1"); //$NON-NLS-1$
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(Long.parseLong(updated));
return getFormat().format(new Date(calendar.getTimeInMillis()));
}
public static Object getOdataValue(Object value) throws ParseException {
DateFormat dateFormat = getFormat();
Date parse = dateFormat.parse(value.toString());
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(parse.getTime());
return "\"/Date(" + calendar.getTimeInMillis() + ")/\""; //$NON-NLS-1$ //$NON-NLS-2$
}
The problem that I got the result of dtae time with UTC for example -
1393358368000 = Tue, 25 Feb 2014 19:59:28 GMT
Your time zone: 2/25/2014 9:59:28 PM GMT+2
The result from this code is 2/25/2014 21:59:28 PM
How I can get the result without time zone ? in this case I want that the result will be ue, 25 Feb 2014 19:59:28 GMT
Can I have tick and got different result with and without UTC ?
It's not entirely clear what you're asking, but if all you want is to make your DateFormat use UTC, that's easy:
public static DateFormat getFormat() {
String systemLocale = System.getProperty("user.language"); //$NON-NLS-1$
Locale locale = new Locale(systemLocale);
DateFormat dateFormat = DateFormat.getDateTimeInstance(
DateFormat.MEDIUM, DateFormat.MEDIUM, locale);
dateFormat.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
return dateFormat;
}
(Any reason you're not using Locale.getDefault() for the default locale, by the way? Or letting the DateFormat pick it itself?)
Also note that you're creating calendars for no reason at all. Your getFormattedValue and getOdataValue methods can be simplified to:
public static Object getFormattedValue(Object value) {
String updated = (strValue).replaceAll(pattern, "$1"); //$NON-NLS-1$
long millis = Long.parseLong(updated);
return getFormat().format(new Date(millis));
}
public static Object getOdataValue(Object value) throws ParseException {
DateFormat dateFormat = getFormat();
Date parsedDate = dateFormat.parse(value.toString());
return "\"/Date(" + date.getTime() + ")/\""; //$NON-NLS-1$ //$NON-NLS-2$
}

how to create Date object from String value

When running through the below code I am getting an UNPARSABLE DATE EXCEPTION.
How do I fix this?
package dateWork;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateCreation {
/**
* #param args
*/
public static void main(String[] args) {
String startDateString = "2013-03-26";
DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
Date startDate=null;
String newDateString = null;
try
{
startDate = df.parse(startDateString);
newDateString = df.format(startDate);
System.out.println(startDate);
} catch (ParseException e)
{
e.printStackTrace();
}
}
}
You used wrong dateformat for month, also you should use the same delimiter as in your date.
If you date string is of format "2013/01/03"
use the same delimiter / for the pattern "yyyy/MM/dd"
If your date string is of format "2013-01-03"
use the same delimiter '-' in your pattern "yyyy-MM-dd"
DateFormat df = new SimpleDateFormat("yyyy/mm/dd");
should be
DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
From SimpleDateFormat Doc
MM---> month in an year
mm---> minutes in hour
MM instead of mm
- instead of /
ie yyyy-MM-dd as you are using - in date string
String startDateString = "2013-03-26";
DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
you are using different pattern than what you are parsing.
either initialize this as DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
or this as String startDateString = "2013/03/26";
also look this article
pass same format string in constructor of SimpleDateFormat("yyyy-mm-dd")
as your string date is "2013-03-26"
if your date is "2013/03/26" use
SimpleDateFormat("yyyy/mm/dd")

how to convert java string to Date object [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 9 years ago.
I have a string
String startDate = "06/27/2007";
now i have to get Date object. My DateObject should be the same value as of startDate.
I am doing like this
DateFormat df = new SimpleDateFormat("mm/dd/yyyy");
Date startDate = df.parse(startDate);
But the output is in format
Jan 27 00:06:00 PST 2007.
You basically effectively converted your date in a string format to a date object. If you print it out at that point, you will get the standard date formatting output. In order to format it after that, you then need to convert it back to a date object with a specified format (already specified previously)
String startDateString = "06/27/2007";
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
Date startDate;
try {
startDate = df.parse(startDateString);
String newDateString = df.format(startDate);
System.out.println(newDateString);
} catch (ParseException e) {
e.printStackTrace();
}
"mm" means the "minutes" fragment of a date. For the "months" part, use "MM".
So, try to change the code to:
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
Date startDate = df.parse(startDateString);
Edit:
A DateFormat object contains a date formatting definition, not a Date object, which contains only the date without concerning about formatting.
When talking about formatting, we are talking about create a String representation of a Date in a specific format. See this example:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTest {
public static void main(String[] args) throws Exception {
String startDateString = "06/27/2007";
// This object can interpret strings representing dates in the format MM/dd/yyyy
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
// Convert from String to Date
Date startDate = df.parse(startDateString);
// Print the date, with the default formatting.
// Here, the important thing to note is that the parts of the date
// were correctly interpreted, such as day, month, year etc.
System.out.println("Date, with the default formatting: " + startDate);
// Once converted to a Date object, you can convert
// back to a String using any desired format.
String startDateString1 = df.format(startDate);
System.out.println("Date in format MM/dd/yyyy: " + startDateString1);
// Converting to String again, using an alternative format
DateFormat df2 = new SimpleDateFormat("dd/MM/yyyy");
String startDateString2 = df2.format(startDate);
System.out.println("Date in format dd/MM/yyyy: " + startDateString2);
}
}
Output:
Date, with the default formatting: Wed Jun 27 00:00:00 BRT 2007
Date in format MM/dd/yyyy: 06/27/2007
Date in format dd/MM/yyyy: 27/06/2007
try
{
String datestr="06/27/2007";
DateFormat formatter;
Date date;
formatter = new SimpleDateFormat("MM/dd/yyyy");
date = (Date)formatter.parse(datestr);
}
catch (Exception e)
{}
month is MM, minutes is mm..
The concise version:
String dateStr = "06/27/2007";
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Date startDate = (Date)formatter.parse(dateStr);
Add a try/catch block for a ParseException to ensure the format is a valid date.
var startDate = "06/27/2007";
startDate = new Date(startDate);
console.log(startDate);

Categories

Resources