I use DateFormat to show Day and Month. DateFormat supports MEDIUM, LONG, FULL all have a year. I want to remove year from this code and how can I achieve this
SimpleDateFormat sdf = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.LONG, locale);
sdf.applyPattern(sdf.toPattern().replaceAll("[^\\p{Alpha}]*y+[^\\p{Alpha}]*", ""));
But I get an error with some locale like:
bo_CN : སྤྱི་ལོ་y MMMMའི་ཙེས་dད
vi: 'Ngày' dd 'tháng' MM 'năm' y
se_SE: d 'de' MMMM 'de' y
You can use SimpleDateFormat:
Date date = new Date();
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("MM/dd");
String dateString = df.format(date);
Output:
01/21
Try with the below code
String shortDate = "dd.MM";
String mediumDate = "MMM dd";
String longDate = "MMMM dd";
String fullDate = "EEEE, MMMM dd";
Locale locale = new Locale("EN");
DateFormat df = new SimpleDateFormat(fullDate, locale); \\Use the required format here
String formattedDate = df.format(new Date());
I have removed the year part and created custom equivalent of
DateFormat.SHORT as shortDate
DateFormat.MEDIUM as mediumDate
DateFormat.LONG as longDate
DateFormat.FULL as fullDate
Related
This question already has answers here:
Java string to date conversion
(17 answers)
Closed 6 years ago.
What are the factors that make a date parsing not parsing the month correctly?
I have this very basic code :
DateFormat format = new SimpleDateFormat("DD/MM/yyyy", Locale.FRENCH);
Date date = format.parse(dateInterv);
dateInterv is a String with the value "14/06/2016" , and when I parse the date, date is in january. Even in debugger I can't see why it transforms 14/06/2016 into 14/01/2016.
Your String must be:
DateFormat format = new SimpleDateFormat("dd/MM/yyyy", Locale.FRENCH);
Date date = format.parse(dateInterv);
because dd is for day of month and DD is for day of year
You can follow any of the following format from SimpleDateFormat.
SimpleDateFormat format = new SimpleDateFormat();
Date curDate = new Date();
format = new SimpleDateFormat("yyyy/MM/dd");
DateToStr = format.format(curDate);
System.out.println(DateToStr);
format = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
DateToStr = format.format(curDate);
System.out.println(DateToStr);
format = new SimpleDateFormat("dd MMMM yyyy zzzz", Locale.ENGLISH);
DateToStr = format.format(curDate);
System.out.println(DateToStr);
format = new SimpleDateFormat("MMMM dd HH:mm:ss zzzz yyyy",
Locale.ITALIAN);
DateToStr = format.format(curDate);
System.out.println(DateToStr);
format = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
DateToStr = format.format(curDate);
System.out.println(DateToStr);
I think you might wanna change it to this:
SimpleDateFormat formatter =new SimpleDateFormat("dd/MM/yyyy", Locale.French);
Date date = formatter.parse(dateInterv);
String formattedDate = formatter.format(date);
I know that to receive date I have to use this code with dd in lowercase:
DateFormat dateFormat0 = new SimpleDateFormat("yyyy/mm/dd HH:MM:SS");
Date dateNow0 = new Date();
System.out.println("Date format: " + dateFormat0.format(dateNow0));
and the output is:
Date format: 2016/28/06 09:03:493
But when I use DD in uppercase in date format. code:
DateFormat dateFormat1 = new SimpleDateFormat("yyyy/mm/DD HH:MM:SS");
Date dateNow1 = new Date();
System.out.println("Date format: " + dateFormat1.format(dateNow1));
The output is:
Date format: 2016/28/66 09:03:494
Why I received different result using capital 'DD' instead of 'dd'?
What the result of 'DD' stands for?
D is Day in year (1-365)
d is day in month (1-31)
See the docs on SimpleDateFormat: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
The proper solution code in Java 7 is:
DateFormat dateFormat3 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat3.format(cal.getTime()));
and in Java 8:
LocalDateTime localDateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
String text = localDateTime.format(formatter);
System.out.println(text);
i have problem of parsing a String into Date when the month contains 3 letters instead of two
use DateFormat in this way:
DateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
System.out.println( df.parse("20-Feb-2006"));
DateFormat df = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
Date date = df.parse("20-Feb-2006");
USe
String strDate = "20-Feb-2006";
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
Date dateStr = formatter.parse(strDate);
I have my date and time stored in string i.e my string contains str ="18/01/2013 5:00:00 pm". How can I convert it to 24 format time in android?
You can use two SimpleDateFormat instances: one to parse the input as a date and a second to format the date as a string with the desired format.
For example, formattedDate in the code below will be 18/01/2013 17:00:00:
String str = "18/01/2013 5:00:00 pm";
SimpleDateFormat input = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
Date dt = input.parse(str);
SimpleDateFormat output = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String formattedDate = output.format(dt); //contains 18/01/2013 17:00:00
Notes:
hh is for Hour in am/pm (1-12) whereas HH is for Hour in day (0-23).
for more formatting options, check the javadoc
try
String str ="18/01/2013 5:00:00 pm";
Date date = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a").parse(str);
str = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(date);
System.out.println(str);
output
18/01/2013 17:00:00
To get AM PM and 12 hour date format use hh:mm:ss a as string formatter WHERE hh is for 12 hour format and a is for AM PM format.
Note: HH is for 24 hour and hh is for 12 hour date format
SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
String newFormat = formatter.format(testDate);
Example
String date = "18/01/2013 5:00:00 pm";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/mm/dd HH:MM:SS");
Date testDate = null;
try {
testDate = sdf.parse(date);
}catch(Exception ex){
ex.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
String newFormat = formatter.format(testDate);
System.out.println(".....Date..."+newFormat);
You can use SimpleDateFormat for that:
SimpleDateFormat dateFormat = new SimpleDateFormat(dd/mm/yyyy HH:mm:ss");
Refer to this which was opposite to your requirement. Just posted the link so you might get the idea of difference that HH and hh makes.
Try using the java SimpleDateFormat class.
Example:
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
df.parse(date);
The upper case HH use a 24h format
Hi I have the following date as String format.
Input
2010-04-20 05:34:58.0
Output I want the string like
20, Apr 2010
Can someone tell me how to do it ?
Try this:
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
DateFormat outputFormat = new SimpleDateFormat("dd, MMM yyyy");
Date yourDate = inputFormat.parse("2010-04-20 05:34:58.0");
String formattedDate = outputFormat.format(yourDate);
You might want to try this:
SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
Date date = inFormat.parse( "2010-04-20 05:34:58.0");
SimpleDateFormat outFormat = new SimpleDateFormat("dd, MMM yyyy");
System.out.println(outFormat.format( date));