This question already has answers here:
How can I change the date format in Java? [duplicate]
(10 answers)
Closed 7 years ago.
I need to change the date format from 2015-04-08 to 08-APR-2015.
It is coming from grails gsp front end.
Before calling oracle package I need to change the format in java.
How to do it.
I use SimpleDateFormat in changing date formats. Try this:
String OLD_FORMAT = "yyyy-MM-dd";
String NEW_FORMAT = "dd-MMM-yyyy";
String oldDateString = "2015-04-08";
String newDateString;
SimpleDateFormat sdf = new SimpleDateFormat(OLD_FORMAT);
Date d = sdf.parse(oldDateString);
sdf.applyPattern(NEW_FORMAT);
newDateString = sdf.format(d);
You mentioned Java in your answer but you tagged JavaScript I've edited your question to show the Java tag. This is a JavaScript soution.
Assuming str is you input:
str.split('-').forEach(function(a,i,b){
result += i===0?(b[2]+'-'):i===1?(((new Date(str)).toUTCString()).split(' ')[2]+'-').toUpperCase():a[0];
});
Ok I already see and answer but still want to add one.I am not sure you want JS or Java solution. You can this using Javascript or Java. Below are both ways:
Javascript
var c = new Date('2015-04-08');
locale = "en-us"
function formatDate(d)
{
var month = d.toLocaleString(locale, { month: "long" });
var day = d.getDate();
day = day + "";
if (day.length == 1)
{
day = "0" + day;
}
return day + '-' + month +'-' + d.getFullYear();
}
And if you want same in Java you do below..
Java
String myDateString = "2015-04-08";
String reqDateString = new SimpleDateFormat("dd-MMM-yyyy").format(myDateString);
Better if done in Java looks more simpler and reliable..
Related
This question already has answers here:
How can I change the date format in Java? [duplicate]
(10 answers)
Closed 5 years ago.
I have a date in String format: 2017-05-10.
I need to transform this string in this 10/05/2017. How i can do?
Which library i can use?
I have tried this but not works
DateFormat df = new SimpleDateFormat("dd-MM-yyyy",Locale.ITALIAN);
Calendar cal = Calendar.getInstance();
cal.setTime(df.parse(data));
Can also be done using the java.time API:
String date = "2017-05-10";
LocalDate ld = LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
String formattedDate = DateTimeFormatter.ofPattern("dd/MM/yyyy").format(ld);
You can try following code:
DateFormat dfto = new SimpleDateFormat("dd/MM/yyyy");
DateFormat dffrom = new SimpleDateFormat("yyyy-MM-dd");
Date today = dffrom.parse("2017-05-10");
String s = dfto.format(today);
I would simply run an internal Java function that comes built in with the String object, described below:
replace(char oldChar, char newChar)
Returns a new string resulting from replacing all occurrences of
oldChar in this string with newChar.
String myDate = df.parseDate(data).toString();
// if that's the right string you mention
// then
cal.setTime(myDate.replace('-', '/'));
They may have to be escaped but since it's char data type my initial thought is no.
This question already has answers here:
Calculating difference in dates in Java
(7 answers)
Closed 6 years ago.
I am new to java. I am having slight confusion regarding date arithmetic in java. I have following scenario, where I want to find differences between two dates :-
java.util.Date objDt1 = getDate1FromSrc1(); // I am obtaining it from src1
java.util.Date objDt2 = getDate2FromOtherSrc(); // I am getting dt2 by other way.
Now, I want to find difference between two dates and the output must be other date object.
So, I have written the following code :-
Calendar objCal1 = Calendar.getInstance();
Calendar objCal2 = Calendar.getInstance();
objCal1.setTime(objDt1);
objCal2.setTime(objDt2);
objCal1.add(Calendar.DAY_OF_MONTH, -objCal2.get(Calendar.DAY_OF_MONTH));
objCal1.add(Calendar.MONTH, -objCal2.get(Calendar.MONTH));
objCal1.add(Calendar.YEAR, -objCal2.get(Calendar.YEAR));
objCal1.add(Calendar.HOUR_OF_DAY, -objCal2.get(Calendar.HOUR_OF_DAY));
objCal1.add(Calendar.MINUTE, -objCal2.get(Calendar.MINUTE));
objCal1.add(Calendar.SECOND, -objCal2.get(Calendar.SECOND));
java.util.Date objDiff = objCal1.getTime();
But, I am getting some wierd results. Eg. If objDt1 is "02/22/2016 09:00:00" and objDt2 is "02/22/2016 11:00:00", then I am expecting objDiff to be "02:00:00" as output, which I am not getting.
Can you suggest me what's wrong I am doing here and what's the right way to approach this problem ?
Thanks in advance.
In order to find the difference between two dates you can simply convert it to long as follows:
java.util.Date objDt1 = getDate1FromSrc1();
java.util.Date objDt2 = getDate2FromOtherSrc();
long date1 = objDt1.getTime();
long date2 = objDt2.getTime();
System.out.println("Difference (In Seconds): " + (date2 - date1)/1000);
If you want this difference to be in HH:mm:ss format then you can do something like this:
public static void main (String[] args) throws Exception
{
SimpleDateFormat sf = new SimpleDateFormat("HH:mm:ss");
java.util.Date objDt1 = getDate1FromSrc1();
java.util.Date objDt2 = getDate2FromOtherSrc();
long date1 = objDt1.getTime();
long date2 = objDt2.getTime();
System.out.println("Difference: " + sf.format(new Date((date2 - date1)/1000)));
}
This question already has answers here:
How to format date and time in Android?
(26 answers)
Closed 7 years ago.
I need to get date format like:
"yyyy-mm-dd am"
or
"yyyy-mm-dd pm"
In Java for Android
I do not need current time, only yyyy-mm-dd plus am/pm
how to make it directly ?
i use the method below to get current date:
private static String getDate(){
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
return sdf.format(c.getTime());
}
You can simply use
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd a");
Output will be
2015-34-20 AM
EDIT
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd a");
this will produce an output of
2015-05-20 AM
You should use these below lines of code...There is only one problem with the SimpleDateFormat is that when ever you change the device language to another language than SimpleDateFormat's string also changes with the language which can create problem in yours application..Therefore i suggest you if you do not want to change the date string according the application language changes than Use below concept which become helpful for you Or Just simply use SimpleDateFormat for not changing the application language to another langauge.
Calendar ci = Calendar.getInstance();
String AM_PM;
if(ci.get(Calendar.AM_PM)==0)
{
AM_PM ="AM";
}
else
{
AM_PM ="PM";
}
String CiDateTime = "" + ci.get(Calendar.YEAR) + "-" +
(ci.get(Calendar.MONTH) + 1) + "-" +
ci.get(Calendar.DAY_OF_MONTH)+" "+AM_PM;
System.out.println("time=========================================="+CiDateTime);
Output:-
time==========================================2015-5-20 PM
You can check the reference for SimpleDateFormat in Android for a list of all the symbols and what they represent.
SimpleDateFormat Reference
I have a YYYYMM date in String format. I want to split it into YYYY and MM and replace the existing YYYY with a new year and concatenate it back in Java.
For example, I have 201201. I want to split it into 2012 and 01 and change 2012 to 2000 and finally get 200001. How do I do it?
I googled it but everyone seemed to have a - or a * in between.
Or if anyone knows a better way to do it (maybe change it into a Date and modify the year), I am all ears.
Any help would be appreciated!
Thanks
Take a look to this code to see if it can help you.
String myDate = "201201";
String myDate1 = myDate.substring(0, 4);
String myDate2 = myDate.substring(4);
myDate1 = "2000"; //Change the logic accordingly with your spec
System.out.println(myDate1 + myDate2);
One uber-simple approach is just to use string manipulation:
String orig = "201201";
String changed = "2000" + orig.substring(4);
EDIT:
A looping example, as per the comment:
// Loop over the inputs
for (String date : getDatesFromFile() {
// Loop over 10 years:
for (int year = 2000; year <= 2010; ++year) {
String newDate = String.valueOf(year) + date.substring(4);
// write newDate to an output file
}
}
try This :
String Str = new String("201201");
System.out.print("Replace Year :" );
System.out.println(Str.replace('2012', '2000'));
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Conversion of Date
I get the date from dialog as: 5-1-2012 - as a String.
I need to convert it to 05-01-2012, string as well. What is the simpliest way to do it?
Do you mean?
String date = "5-1-2012";
if (date.charAt(1) == '-') date = "0" + date;
if (date.charAt(4) == '-') date = date.substring(0,3) + "0" + date.substring(3);
// date is 05-01-2012
SimpleDateFormat s = new SimpleDateFormat("dd/MM/yyyy");
String format = s.format(new Date());
If you plan to use it as a Date object later, I would use the SimpleDateFormat parse() function...
you may test your string to determine whether to use a d-M-yyyy or dd-MM-yyyy pattern for the parser