Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4336706
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:50:37+00:00 2026-05-21T10:50:37+00:00

I am trying to format Date to get a Date of a specific format.

  • 0

I am trying to format Date to get a Date of a specific format. However it is not working. Don’t know why! Here is what I did:

           for(LeaveApply leaveApply : leaveApplyList) {
                SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
                try {
                    leaveApply.setDate(dateFormat.parse(dateFormat.format(leaveApply.getDate())));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }

The output is :

LeaveApply [id=1, user=id [1]; loginName [admin], leaveType=Medical Leave, leaveTime=Half Leave, date=Fri Apr 15 00:00:00 IST 2011]
LeaveApply [id=0, user=id [1]; loginName [admin], leaveType=Weekend, leaveTime=Full Leave, date=Sat Apr 02 00:00:00 IST 2011]
LeaveApply [id=0, user=id [1]; loginName [admin], leaveType=Weekend, leaveTime=Full Leave, date=Sun Apr 03 00:00:00 IST 2011]
LeaveApply [id=0, user=id [1]; loginName [admin], leaveType=Weekend, leaveTime=Full Leave, date=Sat Apr 09 00:00:00 IST 2011]
LeaveApply [id=0, user=id [1]; loginName [admin], leaveType=Weekend, leaveTime=Full Leave, date=Sun Apr 10 00:00:00 IST 2011]
LeaveApply [id=0, user=id [1]; loginName [admin], leaveType=Weekend, leaveTime=Full Leave, date=Sat Apr 16 00:00:00 IST 2011]
LeaveApply [id=0, user=id [1]; loginName [admin], leaveType=Weekend, leaveTime=Full Leave, date=Sun Apr 17 00:00:00 IST 2011]
LeaveApply [id=0, user=id [1]; loginName [admin], leaveType=Weekend, leaveTime=Full Leave, date=Sat Apr 23 00:00:00 IST 2011]
LeaveApply [id=0, user=id [1]; loginName [admin], leaveType=Weekend, leaveTime=Full Leave, date=Sun Apr 24 00:00:00 IST 2011]
LeaveApply [id=0, user=id [1]; loginName [admin], leaveType=Weekend, leaveTime=Full Leave, date=Sat Apr 30 00:00:00 IST 2011]

As you can see the date is not in desired format (dd-MM-yyyy). Any help will be appreciable.
The LeaveApply class is :

public class LeaveApply implements Serializable {

    private static final long serialVersionUID = -7223672229309295181L;
    private long id;
    private User user;
    private String leaveType;
    private String leaveTime;
    private Date date;

    public LeaveApply() {

    }

    public LeaveApply(User user, String leaveType, String leaveTime, Date date) {
        super();
        this.user = user;
        this.leaveType = leaveType;
        this.leaveTime = leaveTime;
        this.date = date;
    }    

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getLeaveType() {
        return leaveType;
    }

    public void setLeaveType(String leaveType) {
        this.leaveType = leaveType;
    }

    public String getLeaveTime() {
        return leaveTime;
    }

    public void setLeaveTime(String leaveTime) {
        this.leaveTime = leaveTime;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    @Override
    public String toString() {
        return "LeaveApply [id=" + id + ", user=" + user + ", leaveType=" + leaveType + ", leaveTime=" + leaveTime + ", date=" + date + "]";
    }    
}

And I am printing the list as :

for (LeaveApply leaveApply : leaveApplyList) {
        System.out.println(leaveApply);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T10:50:38+00:00Added an answer on May 21, 2026 at 10:50 am

    You need to use the SimpleDateFormat to format the string when you print it. What your code does now is:

    • get a timestamp
    • format it
    • parse the formatted string back to a timestamp
    • set the timestamp

    Which is a rather expensive no-op, as a Date object only contains a long representing the timestamp, no formatting information.

    Further on, where you print the timestamp, you use the default date format. You should use your date formatted at that point instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

how to get date in this format: 12/31/9999 11:59:59 PM i am trying it
I am trying to get DateTime to out todays date in %d/%m-%Y' format, but
I'm trying to print out the date in a certain format: NSDate *today =
Im trying to save a bitmap jpg format with a specified encoding quality. However
Trying to make a generic PL/SQL procedure to export data in specific XML format,
I'm in wordpress, trying to format the date output. This is the code I'm
I am trying to use the Get-Date cmdlet to get yesterdays date. I have
I'm trying to find a way to get date and time separately. For date
I am trying get date of call but it's returning value of 1.1.1970(I think
I'm trying to validate a date format in PHP 01/02 Day and Month. Anyway,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.