It can happen in the middle of the night, and often does.
For me, it happens a lot in the morning, when I'm half-awake-half-asleep.
Your brain goes on these excursions, and comes back with some wild creative stuff. Sometimes it is a simple idea, an "ah ha!" that you make a note of and then move on. At other times, it is an idea that will NOT let go, and it starts to really disrupt your life.
"But I don't have time to deal with this!" you think. "I have to study for this test / pick up the kids / finish this report / make this presentation / cook supper / etc."
"Why can't the muse visit at a more appropriate time?" "I have GOT to get some sleep!!"
It is a juggling act. Sometimes we complain that we don't have enough ideas, that we have Writer's Block. Other times we complain that the ideas are plentiful - but they show up at the wrong time! Or, just as bad, they are the Wrong Ideas!
"I wanted to write a love song...and I keep getting this protest song about government bailouts!"
Well, the song is gonna tell you what it is; just go ahead and write it. You won't get any peace until you do, anyway. Mind, it doesn't have to be perfect - but you do have to get it OUT.