I want to format dates with month names with localized label in different languages including Turkish,
how do I set formatted labels for months
Use the SimpleDateFormat constructor taking a Locale.
SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy", new Locale("tr"));
String date = sdf.format(new Date());
System.out.println(date); // 27 Eylül 2011
The Locale accepts an ISO-639-1 language code.
public static void main(String Args[]) {
String text = "Çar, 11 Eyl 2013 19:28:14 +03:00";
try {
Date sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss", new Locale("tr")).parse(text);
SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = DATE_FORMAT.format(sdf);
System.out.println(date);
} catch (ParseException ex) {
Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
}
}
Related
How can I convert 24 hours time format into 12 hours format? I know this question has been asked many times, but my problem is different. My current time is:
Tue Nov 07 18:44:47 GMT+05:00 2017
I just want 6:44 pm from my date time. I tried this:
private void convertTime(String time)
{
try {
final SimpleDateFormat sdf = new SimpleDateFormat("H:mm");
final Date dateObj = sdf.parse(time);
System.out.println(dateObj);
System.out.println(new SimpleDateFormat("K:mm a").format(dateObj));
} catch (final ParseException e) {
e.printStackTrace();
}
}
final SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
Using hh will give you 12 hours format and HH 24 hour format. More details on documentation.
Edit:
Your initial format must be the following in order to parse your date string to a Date object:
final SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM d HH:mm:ss 'GMT'Z yyyy");
final Date dateObj = sdf.parse(time);
After that you can format time to your needs.
From SimpleDateFormat documentation:
"h:mm a": 12:08 PM
So the format you need for:
I just want 6:44 pm from my date time
is:
final SimpleDateFormat sdf1 = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy");
Date date = sdf1.parse(time);
final SimpleDateFormat sdf = new SimpleDateFormat("h:mm a");
String newDateString = sdf.format(date);
try {
final SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
final Date dateObj = sdf.parse(time);
System.out.println(dateObj);
System.out.println(new SimpleDateFormat("K:mm a").format(dateObj));
} catch (final ParseException e) {
e.printStackTrace();
}
I am building an Android App, Fetching Data from API that is build in Ruby On Rails Framework.
Here is my timestamp string
2000-01-01T12:00:00.000Z
I need to convert it to Readable date and time format i.e "Saturday January 1st, 2000 12:00 am"
You can try this:
public static String convertDate(String oldDateString){
String format = "yyyy-MM-dd'T'HH:mm:ss.sssZZZZ";
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date = null;
try {
date = sdf.parse(oldDateString);
} catch (ParseException e) {
// handle exception here !
}
String newFormatString = "MMMM dd, yyyy 'at' HH:mm a";
SimpleDateFormat newFormatter = new SimpleDateFormat(newFormatString);
String newDateString = newFormatter.format(date);
return newDateString;
}
I had the same issue and I tried #real19 answer but it gave me errors.
I came with that then :
public static String convertDate(String oldDateString){
String format = "yyyy-MM-dd'T'HH:mm:ss.sss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date = null;
try {
date = sdf.parse(oldDateString);
} catch (ParseException e) {
// handle exception here !
e.printStackTrace();
}
String newFormatString = "EEEE MMMM dd, yyyy 'at' HH:mm a";
SimpleDateFormat newFormatter = new SimpleDateFormat(newFormatString);
String newDateString = newFormatter.format(date);
return newDateString;
}
Formatting gave me jeudi décembre 22, 2016 at 16:42 PM but you can adjust your DateFormat
Can you please help me to parse the date in the format "2014-09-03T13:13:08Z",
I have tried the format
public static Date getFormatedDate(String date)
{
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss Z");
Date dateConverted = new Date();
try {
dateConverted = df.parse(date);
} catch ( ParseException e ) {
}
return dateConverted;
}
But it doesn't parse.
Your current date format works fine. You need to set Locale
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss Z",
Locale.ENGLISH);
eg:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss Z", Locale.ENGLISH);
Date dateConverted = new Date();
try {
dateConverted = df.parse(date);
} catch (ParseException e) {
System.out.println("Error"+e);
}
System.out.println(dateConverted);
Out put:
Wed Oct 01 12:03:51 IST 2014
I'm trying to convert a String that represents a date stored in SQLITE.
The date was stored into sqlite as follows:
Date date;
date.toString();
According with Java documentation, toString() method:
Returns a string representation of this Date. The formatting is
equivalent to using a SimpleDateFormat with the format string "EEE MMM
dd HH:mm:ss zzz yyyy", which looks something like "Tue Jun 22 13:07:00
PDT 1999". The current default time zone and locale are used. If you
need control over the time zone or locale, use SimpleDateFormat
instead.
Until here, it's fine but, when I try to get the String and convert it to date again, Java throws an exception.
The String comes from sqlite:
Mon Jan 20 18:26:25 BRT 2014
So, I do:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
Date date= sdf.parse("Mon Jan 20 18:26:25 BRT 2014");
What I'm doing wrong?
Thanks.
try this code
String dateString = "here your date";
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(dateString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(convertedDate);
Try this:
String w = "Mon Jan 20 18:26:25 BRT 2014";
SimpleDateFormat pre = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
try{
Date date = pre.parse(w);
System.out.println(sdf.format(date));
}catch(Exception e){
e.printStackTrace();
}
Output:
20/01/2014
Formatter for storing and restoring data value in format dd/MM/yyyy
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
Storing data
String dataAsString = simpleDateFormat.format(date); // 20/01/2014
Restoring data
Date data = simpleDateFormat.parse(dataAsString);
I have a program in which i will get date in the below format "2015-01-17"
Please tell me how can i convert this data to this format JAN 17 2015 .
import java.text.SimpleDateFormat;
public class TestDate {
public static void main(String args[]) throws Exception
{
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
String s1 = "2015-01-17 ";
System.out.println("Result==> "+sdf1.format(sdf2.parse(s1)));
}
}
Change your sdf2 to use the MMM dd yyyy format instead...
String text = "2015-01-17";
try {
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf2 = new SimpleDateFormat("MMM dd yyyy");
Date date = sdf1.parse(text);
System.out.println("Result==> " + sdf2.format(date));
} catch (ParseException exp) {
exp.printStackTrace();
}
Which outputs...
Result==> Jan 17 2015
try this way
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String dateInString = "2015-01-17";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
According to the SimpleDateFormat documentation, you should be able to do that by parsing a date as follows: MMM dd yyyy.