I was reading the story of Joseph in the Bible today and was particularly moved by this very principle in his life. Joseph was a favorite son of his father Jacob and his brothers despised him for it. So one day when they were out in the fields his brothers sold him into slavery to do away with him. Through a series of events, Joseph ends up in prison in Egypt for a number of years. But God uses those days of misfortune and eventually Joseph becomes second in command in the entire land of Egypt next to Pharaoh. God uses Joseph and his godly wisdom to literally save people's lives, including Joseph's own family, during a severe famine in the land.
After all Joseph had been through, I love what he says to his brothers when he reveals himself to them: "I am your brother Joseph, the one you sold into Egypt! And now, do not be distressed and do not be angry with yourselves for selling me here, because it was to save lives that God sent me ahead of you." Joseph certainly had plenty of reason to be angry and upset with his brothers, but instead he sees the larger plan of God in the midst of all that has happened to him.
Now, none of us are promised the outcome of Joseph's life in the midst of our struggles and hardships. For that matter, none of us are even promised the outcome that we desire. But God does promise that in ALL things he works for the good of those who love him. (Romans 8:28) Regardless of what you're going through and regardless of the outcome, we can hope in the promise that God is good, and that he is working on our behalf.
To Him be the glory!