Format for date object with am/pm [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I was wondering how I can just set a time such as 10:30am to a Date object from java.util.date? How would I format this? For example Date date1 = 10:30am.

try with the Calendar class then output using SimpleDateFormat
SimpleDateFormat format = new SimpleDateFormat("HH:mma");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 30);
System.out.println(format.format(calendar.getTime()));

can't do that.Only format to String representations.
SimpleDateFormat simDateFormate = new SimpleDateFormat("hh:mm a");
System.out.println(simDateFormate.format(new Date()));

Related

SimpleDateFormat not working on 1629777600000 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Date format coming from the database: 1629777600000
SimpleDateFormat simpleDateFormatter = new SimpleDateFormat("MM/dd/yyyy");
simpleDateFormat.format(1629777600000);
When using this simple date format I am not able to convert 1629777600000 to a viewable date
SimpleDateFormat simpleDateFormatter = new SimpleDateFormat("MM/dd/yyyy");
System.out.println(simpleDateFormatter.format(1629777600000L));
Print: 08/24/2021
But your code does not compile:
you initialize simpleDateFormatter but use simpleDateFormat (without ter)
1629777600000 is a Long, therefore it needs an L at its end
I think the L problem is just from modifiying the code for the stackoverflow question. But the variable name is maybe a real problem in your code.

Problem: wrong output using Java Calendar class [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I am a Java developer.
I have a question about the Java Calendar library class.
What is my mistake? Is it a bug in Java?
Please explain to me.
public static void main(String[] args) {
Calendar cal2 = Calendar.getInstance();
Calendar cal3 = Calendar.getInstance();
DateFormat dateFormat = new SimpleDateFormat("dd/MM/YYYY");
cal3.add(Calendar.DATE, -1);
String today = (dateFormat.format(cal2.getTime()));
String yesterday = (dateFormat.format(cal3.getTime()));
System.out.println(today);
System.out.println(yesterday);
}
Output:
01/01/2019
Picked up _JAVA_OPTIONS: -Xmx512M
31/12/2019
2019 is as expected in the first line, but I had expected 2018 in the last line.
Use yyyy(Year) for year instead YYYY(Week year). See the doc.

Java Date String to date same format [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
2013-06-24 15:43:00.0 this is Sting
i want parse that to Date
but in same formate
if Month is Month Jan it should be 1 etc
Use the SimpleDateFormat like this:
String dateString = "2013.06.24 15:43:00";
SimpleDateFormat sdfToDate = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
Date date1 = sdfToDate.parse(dateString);
System.out.println(sdfToDate.format(date1));

String to date Conversion in Java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have this code
SimpleDateFormat ft = new SimpleDateFormat ("dd/MM/yyyy");
String sDate="2013-11-13";
String formattedDate=ft.format( ft.parse(sDate));
but this not working. Any help
You need to change the date format like this
SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd");
Edit
comment question : but I need dd/mm/yyyy format?
Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2013-11-13");
String formattedDate = new SimpleDateFormat("dd/MM/yyyy").format(date);
System.out.println(formattedDate);
Change SimpleDate Format to below, and it shall work
SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd");

Add Day and Time parts to a Date Object [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have two date objects.
Date d1;
Date d2;
My program stores in d1, the date in the format 'dd/MM/yyyy' and in d2 in the format 'HH:mm'.
I would now like to combine these information and create a new Date object with the format 'dd/MM/yyyy HH:mm'.
Any pointers on how can I achieve so?
-V
If you are indeed using Date objects, just set the time of d1 to match that of d2.
d1.setHours(d2.getHours());
d1.setMinutes(d2.getMinutes());
Or make a new Date object if you don't want to use d1. Just parse it afterwards with SimpleDateFormat.
Note however that this is deprecated and it's better to use Calendar for this.
First, most of functionality of Date is deprecated. Use Calendar instead. Second, to parse and create string representation of date use SimpleDateFormat.
I hope these tips are enough to start. There are a lot of references in net and javadoc to continue. Good luck.
You cannot use Date for this purpose. You should instead use Calendar (from JDK) or joda-time.
try out this..
String dateValue= new java.util.Date().toString();
String concat like this..
String newDateTime = d1+""+d2+":00";
Try this
DateFormat df1=new SimpleDateFormat("dd/MM/yyyy");
DateFormat df2=new SimpleDateFormat("HH:mm");
Date d1=df1.parse("09/07/2013");
Date d2=df2.parse("14:43");
String d=df1.format(d1).toString()+" "+df2.format(d2).toString();
DateFormat newDate=new SimpleDateFormat("dd/MM/yyyy HH:mm");
System.out.println(newDate.format(newDate.parse(d)));

Categories